如何定位从 UITextField 派生的自定义 UIView 控件
我扩展了 UITextField
,以便我可以使用自定义 myInputView
。但我无法将 myInputView
定位到所需的位置。无论我如何设置 myInputView 的 CGRect 框架,它都会将高度定位在屏幕底部。
请参阅屏幕截图:
正如您在图片中看到的,反转的数字键盘是我的自定义 inputView
,但我希望能够定位它朝向屏幕中间。
MyTextField.h
@interface MyTextField : UITextField {
UIView *myInputView;
}
@property (nonatomic, retain) IBOutlet UIView *myInputView;
@end
MyTextField.m
@implementation MyTextField
@synthesize myInputView;
- (UIView *)inputView {
NSLog(@"Return inputView");
CGRect frame = CGRectMake(0, 200, 320, 215);
[myInputView setFrame:frame];
return myInputView;
}
- (void)dealloc {
[super dealloc];
[myInputView release];
}
@end
I have extended UITextField
so that I can use my custom myInputView
. But I am having trouble positioning the myInputView
to a desire location. Doesn't matter what i do to set the CGRect frame of myInputView
it will position the height be at the bottom of the screen.
See Screenshot:
As you can see in the picture the reversed number pad is my custom inputView
, but I want to be able to position it towards the middle of the screen.
MyTextField.h
@interface MyTextField : UITextField {
UIView *myInputView;
}
@property (nonatomic, retain) IBOutlet UIView *myInputView;
@end
MyTextField.m
@implementation MyTextField
@synthesize myInputView;
- (UIView *)inputView {
NSLog(@"Return inputView");
CGRect frame = CGRectMake(0, 200, 320, 215);
[myInputView setFrame:frame];
return myInputView;
}
- (void)dealloc {
[super dealloc];
[myInputView release];
}
@end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能需要考虑不根据输入视图来实现这一点。设置输入视图可以让系统像对待键盘一样对待它。您不必设置框架,只能设置高度。我建议去掉
UITextField
,并将其替换为UILabel
,并在其上添加UIButton
。将自定义输入视图添加到主视图,并将其放置在屏幕外。按下按钮后,将自定义输入视图动画显示回屏幕上。当用户进行输入时,只需更新标签即可。根本不用担心第一响应者或UITextField.inputView
。You might want to consider NOT implementing this in terms of an input view. Setting the input view lets the system treat it like it treats the keyboard. You DON'T get to set the frame, only the height. I would recommend getting rid of the
UITextField
, and replacing it with aUILabel
with aUIButton
over it. Add your custom input view to the main view, and position it offscreen. When the button is pressed, animate your custom input view back on screen. When the user makes inputs, just update the label. Don't worry about first responder or theUITextField.inputView
at all.