我的 iPad 应用程序使用充当硬件键盘的外部“设备”。但是,在设置中的某个时刻,我需要输入文本,但无法使用“设备”(“设备”不是键盘)。
那么,即使我连接了硬件键盘,有没有办法强制弹出软键盘?
是的。当用户拥有与设备配对的蓝牙扫描仪“键盘”时,我们已经在一些应用程序中执行了此操作。您可以做的是确保您的 textField 有一个 inputAccessoryView,然后自己强制 inputAccessoryView 的框架。这将使键盘显示在屏幕上。
我们向 AppDelegate 添加了以下两个函数。 “inputAccessoryView”变量是我们在应用程序委托中声明的 UIView*:
//This function responds to all textFieldBegan editing
// we need to add an accessory view and use that to force the keyboards frame
// this way the keyboard appears when the scanner is attached
-(void) textFieldBegan: (NSNotification *) theNotification
{
UITextField *theTextField = [theNotification object];
// NSLog(@"textFieldBegan: %@", theTextField);
if (!inputAccessoryView) {
inputAccessoryView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, navigationController.view.frame.size.width, 1)];
}
theTextField.inputAccessoryView = inputAccessoryView;
[self performSelector:@selector(forceKeyboard) withObject:nil afterDelay:0];
}
//Change the inputAccessoryView frame - this is correct for portrait, use a different
// frame for landscape
-(void) forceKeyboard
{
inputAccessoryView.superview.frame = CGRectMake(0, 759, 768, 265);
}
然后在我们的 applicationDidFinishLaunching 中添加了这个通知观察器,这样我们就可以在文本字段开始编辑时收到一个事件
//Setup the textFieldNotifications
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldBegan:) name:UITextFieldTextDidBeginEditingNotification object:nil];
希望有帮助!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)