ios - 将 UITextField 键盘置于最前面

标签 ios iphone 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@domain.com";

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

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

最佳答案

所以事实证明,为“评论”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/

相关文章:

ios - settings.bundle xcode 6 中的子 Pane

android - iOS 和 Android 上的 Kotlin/Native 垃圾收集

iphone - 在 obj-c 中使用正则表达式在字符串中搜索模式

iphone - xcode 计算 JSON 项目

ios - 从核心数据中删除特定对象 (Swift)

javascript - 在 iPhone 上禁用 "block selection"

iphone - 如何使用 AFNetworking 2.0 下载图像?

swift - UIColletionViewCell 中的 UITextField 成为 FirstResponder

objective-c - 仅在 viewDidLoad 上使用 'self.' 更新 UITextField

ios - UITextField shouldChangeCharactersInRange 没有检测到第一个字符