LukeSideWalker LukeSideWalker - 2 years ago 139
Swift Question

CAGradientLayer causes subviews not being visible

Hello Masters of iOS and Swift,

after two frustrating days, I desperately decided to ask here. The problem:

When adding a CAGradientLayer, the subviews aren't shown.


  • I have made a method to add a CAGradientLayer in an extension of

  • I simply call the method on any view and this itself works perfectly

  • But if I try to use this method for a UIView in a viewhierarchy (as a background) unfortunately all subviews aren't visible anymore, the
    gradient seems to "overrender" all subviews

  • if I don't call the "addGradient" method on the container view, all subviews are shown properly

  • amazing detail: Although the subviews aren't visible, they are somehow present and "active" (e.g. a "invisible" UIButton fires")

  • I am using Autolayout

Any idea would be appreciated!
Thanks in advance.

Answer Source


Not sure but you can always try to add your layer below all other layer so it wont cover existing subViews. Try this, in your extension where you add layer to view

self.layer.insertSublayer(layer, atIndex: 0)

Tried adding ImageView as subView worked fine :) Should solve your problem as well.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download