Setup:
I have a View Controller
that consists of a View
and a Container View
.
The View
(Orange) is pinned
to top
0, left
0, and right
0.
The Container View
(Gray) is pinned
to bottom
0, left
0, and right
0.
The View
's Bottom Space to: Container View
= 0
The View
's Proportional Height to Container
View = 1
Desired Results:
I would like to add gradient to the background of the View
(Orange)
Tried:
I'm using Auto-layout with class sizes to get different behavior on different screen.
Code:
class ViewController: UIViewController {
@IBOutlet weak var graphView: UIView!
@IBOutlet weak var containerView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
let backgroundColor = CAGradientLayer().graphViewBackgroundColor()
backgroundColor.frame = self.graphView.frame
self.graphView.layer.addSublayer(backgroundColor)
}
I have a category:
extension CAGradientLayer {
func graphViewBackgroundColor() -> CAGradientLayer {
let topColor = UIColor(red: (160/255.0), green: (160/255.0), blue: (160/255.0), alpha: 1)
let bottomColor = UIColor(red: (52/255.0), green: (53/255.0), blue: (52/255.0), alpha: 1)
let gradientColors: [CGColor] = [topColor.CGColor, bottomColor.CGColor]
let gradientLocations: [Float] = [0.0, 1.0]
let gradientLayer: CAGradientLayer = CAGradientLayer()
gradientLayer.colors = gradientColors
gradientLayer.locations = gradientLocations
return gradientLayer
}
}
Result:
As you can see gradient did not cover the entire View
.
Question: How can I get the gradient to cover the entire View
Update:
When I place the code in viewDidLayoutSubviews()
It looks weird when I rotate: