Last active
January 7, 2018 16:23
-
-
Save jazzedge/2359912828dd3fc3e3352cb3fb76c853 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
See: https://marcosantadev.com/calayer-auto-layout-swift/ | |
Works when you change orientation | |
import UIKit | |
class WelcomeViewController: UIViewController { | |
var gradientLayer: CAGradientLayer! | |
@IBOutlet weak var bkView: UIView! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
gradientLayer = setGradient() | |
bkView.layer.addSublayer(gradientLayer) | |
} | |
override func viewDidLayoutSubviews() { | |
super.viewDidLayoutSubviews() | |
gradientLayer.frame = view.bounds | |
} | |
} | |
extension WelcomeViewController { | |
func setGradient() -> CAGradientLayer { | |
// let screenSize = UIScreen.main.bounds | |
// let screenWidth = screenSize.width | |
// let screenHeight = screenSize.height | |
let colorTop = UIColor(hex: "e16f00") // Light orange | |
let colorBtm = UIColor(hex: "e15700") // Dark orange | |
let layer = CAGradientLayer() | |
//layer.frame = CGRect(x: 0, y: 0, width: screenWidth, height: screenHeight ) | |
layer.colors = [colorTop.cgColor, colorBtm.cgColor] // [UIColor.red.cgColor, UIColor.black.cgColor] | |
//bkView.layer.addSublayer(layer) | |
return layer | |
} | |
} | |
// MARK: Extension to UIColor that converts six digit hex color (excludes hash sign and alpha value) into rgb value | |
extension UIColor { | |
convenience init(hex: String) { | |
let scanner = Scanner(string: hex) | |
scanner.scanLocation = 0 | |
var rgbValue: UInt64 = 0 | |
scanner.scanHexInt64(&rgbValue) | |
let r = (rgbValue & 0xff0000) >> 16 | |
let g = (rgbValue & 0xff00) >> 8 | |
let b = rgbValue & 0xff | |
self.init( | |
red: CGFloat(r) / 0xff, | |
green: CGFloat(g) / 0xff, | |
blue: CGFloat(b) / 0xff, alpha: 1 | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment