Setup:
I have a View Controller , which consists of a View and a Container View .

View (orange) - from pinned to top 0, left 0 and right 0.
Container View (gray) - from pinned to bottom 0, left 0 and right 0.
View Bottom Space to: Container View = 0
View Proportional Height to Container View = 1
Desired results: I would like to add a gradient to the background of the View (orange)
I tried:
I use Auto-layout with class sizes to get different behavior on a 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, the gradient does not cover the entire View .
Question: How can I get a gradient to cover the whole View
Update:
When I put the code in viewDidLayoutSubviews() It looks weird when I rotate:
