OGeek|极客世界-中国程序员成长平台

标题: ios - 将 UITextField 键盘放在前面 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 16:00
标题: ios - 将 UITextField 键盘放在前面

我创建了一个自定义的 UIAlertView(从头开始,没有以任何方式子类化)类,效果很好,但是当警报有一个文本字段时我遇到了问题。在这种特殊情况下,当我点击电子邮件地址字段时,键盘会出现在我的弹出警报下方。调用此弹出窗口的 View 也有一个 UITextField(一个注释框)。我注意到只有在点击主视图文本字段(评论框)后点击弹出文本字段(电子邮件地址)时才会出现此问题。文本字段未使用相同的委托(delegate)。

enter image description here

有没有办法将键盘强制放在 View 层次结构的前面?

更新: 将文本字段添加到弹出窗口的代码(这是我的警报类 PopupDialog.m 中的一个)

self.textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 100, 34)];
_textField.font = [UIFont fontWithName"GillSans" size:18];
_textField.borderStyle = UITextBorderStyleRoundedRect;
_textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
_textField.backgroundColor = [UIColor white];
_textField.delegate = self;
_textField.placeholder = @"[email protected]";

它稍后被添加到按钮上方的弹出 View 中。半透明的黑色背景和弹出 View 最终通过以下方式添加到 View 层次结构中:

[[[[UIApplication sharedApplication] windows] lastObject] addSubview:background];
[[[[UIApplication sharedApplication] windows] lastObject] addSubview:self];



Best Answer-推荐答案


因此,弹出窗口中的“电子邮件”UITextField 使用了与“评论”UITextField(弹出窗口下方)相同的键盘.我不得不移动这个键盘 View 后面的弹出窗口和半透明黑色背景。

int count = [[[UIApplication sharedApplication] windows] count];
[[[[UIApplication sharedApplication] windows] objectAtIndex:count - 2] addSubview:background];
[[[[UIApplication sharedApplication] windows] objectAtIndex:count - 2] addSubview:self];

count - 1 处的 View 是键盘 View 。

关于ios - 将 UITextField 键盘放在前面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19578137/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4