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?
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.
What worked for me was to insert the layer at a specific index like this:
view.layer.insertSublayer(backgroundLayer, at: 0)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With