【发布时间】:2014-10-21 22:53:34
【问题描述】:
我有一个UITextField,它在其inputView 中使用自定义UIPickerView 将用户限制为几个选项,其功能类似于下拉列表。当与通过蓝牙连接的硬件键盘一起使用时,硬件键盘会覆盖(并隐藏)我的inputView。但是,关联的inputAccessoryView 仍然存在。当按下我的硬件键盘上的“键盘”按钮(通常会显示香草UITextField 的默认屏幕键盘)时,一切都按预期工作。 (我的意思是我的 inputView 再次可见。)在 iOS 的早期版本(即 7 及更低版本)中,inputView 在调用时始终可见。
我通过将UITextField 的UIKeyboardType 从UIKeyboardTypeDefault 设置为UIKeyboardTypeTwitter 暂时解决了这个问题,但这仅在硬件键盘未被识别为特定UIKeyboardType 的默认设置时才有效.代码是pickerTextField.keyboardType = UIKeyboardTypeTwitter;(参见下面代码的大约 2/3。)经过进一步测试,这个部分解决方案并不像看起来那么有用。
如何在使用硬件键盘时在所有情况下正确显示我的inputView?
相关代码sn-p:
-(int)setUpPickerWheelAtXLoc:(int)xLoc andYLoc:(int)yLoc {
int qwidth = width - (xLoc - FORMBORDERLEFT);
// Set up inputView (pickerView) to replace the keyboard
self.pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(xLoc, yLoc, qwidth, height)];
self.pickerView.delegate = self;
self.pickerView.dataSource = self;
self.pickerTextField = [[UITextField alloc] initWithFrame:CGRectMake(xLoc, yLoc, qwidth, 32)];
self.pickerTextField.delegate = self;
pickerTextField.borderStyle = UITextBorderStyleRoundedRect;
pickerTextField.layer.borderWidth = 0.3f;
pickerTextField.layer.borderColor = [[UIColor blackColor] CGColor];
pickerTextField.textColor = [UIColor blackColor];
pickerTextField.font = [UIFont systemFontOfSize:XXLARGEFONTSIZE];
pickerTextField.placeholder = @"Tap to choose from list...";
// Add pickerView as inputView
pickerTextField.inputView = self.pickerView;
pickerTextField.keyboardType = UIKeyboardTypeTwitter; // Suggested temporary solution
// Setup inputAccessoryView (Done button)
UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[doneButton addTarget:self action:@selector(pickerDoneButtonPressed) forControlEvents:UIControlEventTouchUpInside];
[doneButton setTitle:@"Done" forState:UIControlStateNormal];
doneButton.titleLabel.font = [UIFont systemFontOfSize:XXLARGEFONTSIZE];
doneButton.frame = CGRectMake(0,0,100,44);
[doneButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
doneButton.contentEdgeInsets = UIEdgeInsetsMake(0,0,0,20);
doneButton.backgroundColor = [UIColor lightGrayColor];
pickerTextField.inputAccessoryView = doneButton;
return xLoc + qwidth;
}
附加信息:在检查了其他几个地方后,我确认至少有一款 Apple 产品也出现了这种情况。 (在 8.0.2 中的 iPhone 6 上的“创建新 Apple ID”应用程序/页面中。查看月份和日期的出生日期选择器视图。)
【问题讨论】:
-
我有理由怀疑这与安装的自定义键盘(如 Swype)干扰的原因相同。见stackoverflow.com/q/26020956/650365
-
我也想为此找到解决方案。无论是否存在硬件键盘,都应显示自定义输入视图。