为什么我们不能把 UIView 的子视图放在它的 CALayer 前面? [英] Why can't we put subviews of a UIView in front of its CALayer?

查看:27
本文介绍了为什么我们不能把 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?

推荐答案

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

For anyone who struggled in this. The reason your subview is under parent's layer might be because you add subview first, then add parent's layer latter.

你做了什么:

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 只是在本机实现下将子层添加到父视图.

To answer your question, the only thing that decide the z order between views are the order you adding either layer or view. I think addSubview simply add sublayer to parent view under native implementation.

这篇关于为什么我们不能把 UIView 的子视图放在它的 CALayer 前面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆