Skip to content

Instantly share code, notes, and snippets.

@marcos1262
Last active August 3, 2018 11:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcos1262/7f03ec16a47af105a8cd79cb05e127b0 to your computer and use it in GitHub Desktop.
Save marcos1262/7f03ec16a47af105a8cd79cb05e127b0 to your computer and use it in GitHub Desktop.
Example of gradient on iOS
override func draw(_ rect: CGRect) {
self.layer.insertSublayer(AppDelegate.mainGradient(self.frame.width, self.frame.height), at: 0)
}
static func mainGradient(_ width: CGFloat, _ height: CGFloat) -> CAGradientLayer {
let gradient = CAGradientLayer()
gradient.frame = CGRect(x: 0, y: 0, width: width, height: height)
gradient.startPoint = CGPoint(x: 0, y: 1)
gradient.endPoint = CGPoint(x: 1, y: 0)
gradient.colors = [UIColor(rgb: 0x6441a5).cgColor,
UIColor(rgb: 0x2a0845).cgColor]
return gradient
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// prepare navigationBar
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.isTranslucent = true
self.navigationController?.navigationBar.backgroundColor = UIColor.clear
// white largeTitle and statusBar
self.navigationController?.navigationBar.largeTitleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
UIApplication.shared.statusBarStyle = .lightContent
// apply gradient
self.view.layer.addSublayer(
AppDelegate.mainGradient(UIApplication.shared.statusBarFrame.width,
UIApplication.shared.statusBarFrame.height + self.navigationController!.navigationBar.frame.height))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment