how to add a UITextField to a CALayer and make it work

旧时模样 提交于 2019-12-13 04:17:44

问题


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

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?


回答1:


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.

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.



来源:https://stackoverflow.com/questions/21728564/how-to-add-a-uitextfield-to-a-calayer-and-make-it-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!