如何将 UITextField 添加到 CALayer 并使其工作 [英] how to add a UITextField to a CALayer and make it work

查看:29
本文介绍了如何将 UITextField 添加到 CALayer 并使其工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在谷歌上搜索,我找到了这个.但我不确定答案是否正确.

I searched on google and I found this. but I'm not sure the answer is true.

所以我需要的是将 UITextField 添加到 CALayer 并使其工作我使用了这个方法但它不起作用

So what I need is to add UITextField into a CALayer and make it work I used this method but its not working

[layer addSubLayer:textField.layer];

有什么想法可以解决这个问题吗?

Any ideas to solve this problem?

推荐答案

简短的回答是,你不能.层是较低级别的对象.视图有图层,但图层不能有视图.

The short answer is, you can't. Layers are lower-level objects. Views have layers, but layers can't have views.

如果您要添加另一个视图的图层作为当前视图图层的子图层,则视图级别将不知道它的图层正在其他地方托管.视图不会是视图层次结构的一部分,因此它不会认为它需要绘制自己.而且,除非您在某处维护对它的强引用,否则它将被释放,导致您添加的层被释放,并创建一个僵尸.

If you were to add another view's layer as a sublayer of your current view's layer, the view level would have no knowledge that it's layer was being hosted somewhere else. The view would not be part of the view hierarchy, so it would not think it needs to draw itself. And, unless you maintain a strong reference to it somewhere, it would be deallocated, causing the layer that you've added to be deallocated, and creating a zombie.

视图包含视图.层包含层.将它们分开.如果您想要图层中的文本内容,您可以使用 CATextLayer,尽管它们看起来不如文本 UI 对象,因为它们不进行子像素抗锯齿.我尝试使用文本图层但最终放弃了它们,因为它们看起来很糟糕.

Views contain views. Layers contain layers. Keep them separate. If you want text contents in layers, you can use a CATextLayer, although they don't look as good as text UI objects because they don't do sub-pixel anti-aliasing. I tried using text layers and ended up abandoning them because they looked really bad.

这篇关于如何将 UITextField 添加到 CALayer 并使其工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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