为什么添加子层会与子视图重叠? [英] Why adding sublayer overlaps subviews?
问题描述
例如,我有一个带有UILabel的视图作为子视图.如果在某个时候将具有某种背景颜色的子层添加到该视图的层,则该标签将消失.有人可以解释这种行为吗?
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?
推荐答案
将UILabel添加为视图的子视图时,它会将UILabel的图层添加为视图层的子层.因此,当您在视图的图层中添加另一个子图层时,它将位于UILabel图层的顶部.
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.
您可以在添加UILabel之前添加背景层,也可以执行以下操作:
You can either add your background layer before you add the UILabel or do:
快速
view.layer.insertSublayer(backgroundLayer, below: yourLabel.layer)
目标C
[view.layer insertSublayer:backgroundLayer below:yourLabel.layer]
它应该将背景放在标签后面.
and it should put the background behind the label.
这篇关于为什么添加子层会与子视图重叠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!