Making Gradient Background in Swift - The Right Way

后端 未结 1 659
臣服心动
臣服心动 2021-01-29 08:07

This is my code for a gradient background. When I apply it on my view, it hides all my stuff on that VC, like it\'s on the front and everything else is pushed back. How can I ma

相关标签:
1条回答
  • 2021-01-29 08:38

    like it is on the front and everything else is pushed back

    Because it is on the front. Think about this line of your code:

    layer.addSublayer(gradientLayer)
    

    That puts the gradient layer in front of all other sublayers of this layer. Subviews of a view are displayed by sublayers of its layer, so the gradient layer covers this view's current subviews.

    You can insert your layer all the way at the back if you like (insertSublayer:atIndex:). The elegant solution, though, is to implement the layerClass class method for this view to return a CAGradientLayer. Now you won't have to add a gradient to your GradientView; instead, your GradientView is a gradient and you can just configure that gradient as desired.

    0 讨论(0)
提交回复
热议问题