问题
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