Apply Gradient to View
class Testing: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
setGradientBackground()
}
func setGradientBackground() {
let colorTop = UIColor(red: 255.0/255.0, green: 149.0/255.0, blue: 0.0/255.0, alpha: 1.0).cgColor
let colorBottom = UIColor(red: 255.0/255.0, green: 94.0/255.0, blue: 58.0/255.0, alpha: 1.0).cgColor
let gradientLayer = CAGradientLayer()
gradientLayer.colors = [ colorTop, colorBottom]
gradientLayer.locations = [ 0.0, 1.0]
gradientLayer.frame = self.view.bounds
self.view.layer.addSublayer(gradientLayer) // --> This will hides the background view
}
-->Some times it will apply full view, better to apply gradient in viewDidLoad()
replace
self.view.layer.addSublayer(gradientLayer)
with self.view.layer.insertSublayer(gradientLayer, at: 0)
, this will put the layer "below" all others --------------------------------------------------------------------------------------------------------------
background gradient to fill Landscape mode
-> some times gradient view will not cover total view when rotate screen .
we can solve this one to add below code
override func willAnimateRotation(to toInterfaceOrientation: UIInterfaceOrientation, duration: TimeInterval) {
self.view.layer.sublayers?.first?.frame = self.view.bounds
}
No comments:
Post a Comment