Why can't we put subviews of a UIView in front of its CALayer?

时间:2019-03-19 15:06:04

标签: ios uiview calayer

As the title of this post, I found the answer regarding "Can we put subviews of a UIView in front of its CALayer" in this post, but I cannot find the answer to why this is so. In my opinion, having the view as the subview of the layer's view only suggests "stronger" order between the subview and the layer. Can somebody explain this to me?

2 个答案:

答案 0 :(得分:0)

每个视图都由CALayer支持,并且层树是视图树的超集。因此,您添加到视图的每个子视图都会将其相应的图层添加到其父视图的图层中。由于其父层和层的子层是从树的顶部向下渲染的,因此您无法更改渲染顺序。

答案 1 :(得分:0)

对于任何在这方面挣扎的人。 你的子视图在父层下的原因可能是因为你先添加子视图,然后再添加父层。

你做了什么:

parentView.addSubview(childView)
parentView.layer.addSublayer(someLayer)
// This makes someLayer on top of subview.

你想要的:

parentView.layer.addSublayer(someLayer)
parentView.addSubview(childView)
// This makes the subview on top of someLayer

为了回答您的问题,决定视图之间 z 顺序的唯一因素是您添加图层或视图的顺序。 我认为 addSubview 只是在本机实现下将子层添加到父视图。