0

I am trying to add a gradient to a view. However the view is positioned using Auto Layout, and therefore the gradient can no longer be applied to the object. What should I do?

This is the gradient function:

func setGradientBackground() {

  let gradient = CAGradientLayer()
  gradient.frame = bounds
  gradient.colors = [
  UIColor(red: 143/255, green: 162/255, blue: 217/255, alpha:1).cgColor,
  UIColor(red: 220/255, green: 141/255, blue: 129/255, alpha: 1).cgColor
  ]
  gradient.locations = [0.0, 1.0]
  gradient.startPoint = CGPoint(x:0, y:0)
  gradient.endPoint = CGPoint(x:1, y:1)

  layer.insertSublayer(gradient, at: 0)
 }

And this is the code for adding the Object

let testView = UIView()

testView.setGradientBackground()
self.view.addSubview(testView)

testView.translatesAutoresizingMaskIntoConstraints = false

testView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
testView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
testView.heightAnchor.constraint(equalToConstant: 100).isActive = true
testView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
rmaddy
  • 314,917
  • 42
  • 532
  • 579
David Sundström
  • 105
  • 1
  • 1
  • 9
  • 1
    Use this custom gradient view https://stackoverflow.com/questions/24380535/how-to-apply-gradient-to-background-view-of-ios-swift-app/37243106#37243106 – Leo Dabus Aug 02 '18 at 19:57
  • This is the answer https://stackoverflow.com/questions/3679858/calayer-autoresizing-on-iphone-ipad-how/4111917#4111917 – Leonid Usov Aug 02 '18 at 20:15
  • `testView` has frame equal to .zero so gradient layer will also have the same frame. Hence you will not see anything. Initialize `testView` with some non zero frame to see the effect. – Kamran Aug 02 '18 at 20:18
  • @Kamran Yeah true, but if I initialize the testView with some temporary non zero properties, then my actual constraints will not work? Or could your give an example of how to fix the code ? – David Sundström Aug 02 '18 at 20:43
  • Yes, I can provide the fix if you can share some more information like who is the parent where this code is added, ViewController or UIView? From where you are calling this code? – Kamran Aug 02 '18 at 20:52
  • @Kamran Great! The "setGradientBackground" is a function inside a UIKit extension. And all the "Testview" content is inside the viewdidload of a regular Viewcontroller. Tell me if you want more info, like a screenshot from the code or something/If I should upload all the code? – David Sundström Aug 02 '18 at 20:56
  • 1
    Remove `testView.setGradientBackground()` from `viewDidLoad`. Make `testview` as class member and override this method in `ViewController`. `override func viewDidLayoutSubviews() { super.viewDidLayoutSubviews() testView.setGradientBackground() }`. – Kamran Aug 02 '18 at 21:08
  • @kamran Awesome! Thanks for the help :) – David Sundström Aug 02 '18 at 21:20
  • `viewDidLayoutSubviews` will run every time there is a layout change, so you don't want to add a new gradient every time. Check if you've already added the gradient and just update its frame on second and later calls. – vacawama Aug 02 '18 at 21:23
  • @vacawama Sounds reasonable, how would I do that? – David Sundström Aug 02 '18 at 21:54
  • 1
    Make a subclass of UIView and override `layoutSubviews`. Check out what this person did here: https://stackoverflow.com/a/51543676/1630618 – vacawama Aug 03 '18 at 01:21
  • @vacawama, Perfect, thanks :) – David Sundström Aug 03 '18 at 08:50

0 Answers0