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
 
    