2

I am trying to create a simple gradient for the background of a UIViewController. My code is:

    let gradientLayer = CAGradientLayer()
    gradientLayer.frame = view.bounds
    let bottomColor = UIColor(hue: 208 / 360, saturation: 82 / 100, brightness: 0.9, alpha: 1)
    let topColor = UIColor(hue: 208 / 360, saturation: 41 / 100, brightness: 0.9, alpha: 1)
    gradientLayer.colors = [bottomColor, topColor]
    gradientLayer.startPoint = CGPoint(x: 0.5, y: 0.1)
    gradientLayer.endPoint = CGPoint(x: 0.5, y: 0.0)

    view.layer.addSublayer(gradientLayer)

The gradient does not show. When I look in the view debugger, I do not see it. I check the frame of the view and it looks ok

x: 0, y:0, width: 375, height: 667

I've tried the view.layer.insert at method as well, but that did not work. If I do

view.layer.backgroundColor.orange

then I do see an orange background. Am I missing something?

1
  • What method are you adding the gradient layer in? Commented Feb 4, 2018 at 23:44

1 Answer 1

1

Change

gradientLayer.colors = [bottomColor, topColor]

to

gradientLayer.colors = [bottomColor.cgColor, topColor.cgColor]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.