如何在 CALayer 中使用 UITextField? [英] How can I use UITextField inside a CALayer?

查看:30
本文介绍了如何在 CALayer 中使用 UITextField?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 CALayer 中添加一个文本字段,但我没有等待.

I want to add a textfield inside a CALayer but I don't find a wait to do it.

代码:

-(void)drawInContext:(CGContextRef)ctx
{        
    NSLog(@"OK");
    UITextField *field = [[UITextField alloc] initWithFrame:CGRectMake(0,120, self.frame.size.width, 100)];
    [field setText:@"Test"];
    [self addSublayer:field.layer];
}

任何想法、建议、好的做法:) ?

Any idea, suggestion, good pratices :) ?

推荐答案

您无法设法将 UITextField(UIView 的子类)作为 CALayer 的子(子层),它需要一个对象具有 CALayer 祖先.

There is no way you will manage to put a UITextField, which is a subclass of UIView, as a child (sublayer) of a CALayer, which expects an object with a CALayer ancestor.

但是你当然可以找到CALayer的superview,它继承了UIView,把UITextField放在了显示时间的好位置,将CALayer设置为UITextField的delegate,让你输入的内容保持同步(请参阅 UITextFieldTextDidChangeNotification 通知或 -textFieldDidEndEditing: UITextField 中的委托方法).

However you certainly can find the superview of the CALayer, which inherits UIView and put the UITextField at the good place for the time it is displayed, and set the CALayer as the delegate of the UITextField to keep the content synchronised as you type (see UITextFieldTextDidChangeNotification notification or -textFieldDidEndEditing: delegate method in UITextField).

这是在 Quartz Composer 中的外观(使用 Cocoa,而不是 Cocoa Touch)

Here is how it looks in Quartz Composer (using Cocoa, not Cocoa Touch)

这篇关于如何在 CALayer 中使用 UITextField?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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