如何定位从 UITextField 派生的自定义 UIView 控件

发布于 2024-10-10 12:57:06 字数 961 浏览 2 评论 0原文

我扩展了 UITextField,以便我可以使用自定义 myInputView。但我无法将 myInputView 定位到所需的位置。无论我如何设置 myInputView 的 CGRect 框架,它都会将高度定位在屏幕底部。

请参阅屏幕截图:

http://lh3.ggpht.com/_h3ntmj5-XE8/TSK_zj6GfcI/AAAAAAAAAv8/wNWvuXS5Qk8/s640/Screen%20shot %202011-01-03%20at%2010.33.00%20PM.png

正如您在图片中看到的,反转的数字键盘是我的自定义 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:

http://lh3.ggpht.com/_h3ntmj5-XE8/TSK_zj6GfcI/AAAAAAAAAv8/wNWvuXS5Qk8/s640/Screen%20shot%202011-01-03%20at%2010.33.00%20PM.png

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

能怎样 2024-10-17 12:57:06

您可能需要考虑不根据输入视图来实现这一点。设置输入视图可以让系统像对待键盘一样对待它。您不必设置框架,只能设置高度。我建议去掉 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 a UILabel with a UIButton 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 the UITextField.inputView at all.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文