Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why adding sublayer overlaps subviews?

Tags:

ios

For example I have a view with UILabel as subview. If at some time I add sublayer with some background color to this view's layer, then this label will disappear. Can someone explain this behavior?

like image 458
rabbitinspace Avatar asked Feb 27 '15 12:02

rabbitinspace


2 Answers

When you add your UILabel as a subview of your view it is adding your UILabel's layer as a sublayer of your view's layer. So when you add another sublayer to your view's layer it will be on top of your UILabel's layer.

You can either add your background layer before you add the UILabel or do:

Swift

view.layer.insertSublayer(backgroundLayer, below: yourLabel.layer)

Objective C

[view.layer insertSublayer:backgroundLayer below:yourLabel.layer]

and it should put the background behind the label.

like image 80
dan Avatar answered Nov 13 '22 05:11

dan


What worked for me was to insert the layer at a specific index like this:

view.layer.insertSublayer(backgroundLayer, at: 0)

like image 42
denis_lor Avatar answered Nov 13 '22 04:11

denis_lor