![](/img/trans.png)
[英]Programmatically show soft keyboard on iPhone in a PhoneGap application?
[英]Show iPhone soft keyboard even though a hardware keyboard is connected
我的 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];
希望有帮助!
使用当前的 SDK 无法做到这一点。 请通过Bug Reporter告知 Apple。
此处的解决方案不适用于 iOS 13 或与 App Store 不兼容,因此我通过创建自己的软键盘解决了该问题。 这是非常基本的,但有效。 随意贡献!
你所要做的就是将 SoftKeyboardView.swift 添加到你的项目中,然后在某个地方(例如 appDidFinishLaunching)点击单例:
用法:
SoftKeyboardManager.shared.disabled = false
由于我遇到了同样的问题,我找到的最接近的解决方案是使用 Erica Sadun 的名为KeysPlease的应用程序,该应用程序可通过 cydia 和 modmyi 获得。 它的描述是“即使连接到 BT kb,也使用软 kb。”。
此外,我发现如果您还连接了物理键盘,就我而言,通过 iPad 键盘文档,您可以使用似乎映射到蓝牙键盘上的弹出键的键来调出键盘。 也许有一种方法可以注入这个键,就像在连接的键盘上按下它一样?
我真的希望有一个更官方的编码解决方案。
当我的应用程序连接蓝牙设备时,键盘不会显示。我尝试像布赖恩罗宾斯所说的那样强制设置 inputAccessoryView 的框架。 它没有用。
然后我用了一个愚蠢的方法来解决。我发现当我再次单击textfield或textview时,键盘会显示出来。 所以我只需要在 textfield 或 textview 中模拟一次触摸,它就可以工作。
如果你想做一些模拟触摸,检查这个。 https://github.com/HUYU2048/PTFakeTouch
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.