即使连接了硬件键盘,也会显示iPhone软键盘

问题描述 投票:22回答:4

我的iPad应用程序使用外部“设备”充当硬件键盘。但是,在设置的某些时候,我需要输入文本而我不能使用“设备”(“设备”不是键盘)。那么,即使我连接了硬件键盘,有没有办法强制弹出软键盘?

iphone cocoa-touch ipad uikit keyboard
4个回答
20
投票

是。我们已经在我们的一些应用程序中完成了这项工作,当用户将蓝牙扫描仪“键盘”与设备配对时。你可以做的是确保你的textField有一个inputAccessoryView,然后自己强制inputAccessoryView的框架。这将导致键盘显示在屏幕上。

我们在AppDelegate中添加了以下两个函数。 'inputAccessoryView'变量是我们在app delegate中声明的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];

希望有所帮助!


1
投票

使用当前的SDK无法做到这一点。请通过the Bug Reporter告知Apple。


0
投票

由于我遇到同样的问题,我找到的最接近的解决方案是使用Erica Sadun的应用程序KeysPlease,它可以通过cydia和modmyi获得。它的描述是“即使连接到BT kb也使用软kb”。

另外我发现如果你还有一个物理键盘,在我的情况下通过iPad键盘文档,你可以使用一个键来调出键盘,这个键似乎映射到蓝牙键盘上的弹出键。也许有一种方法可以注入这个键,好像它是在连接的键盘上按下的一样?

我真的希望有一个更正式的编码解决方案。


0
投票

当我的应用程序连接蓝牙设备时,键盘将无法显示。我尝试设置强制inputAccessoryView的框架,如Brian Robbins所说。它没用。

然后我用一种愚蠢的方式来解决。我发现当我再次单击textfield或textview时,键盘会显示出来。所以我只需要在textfield或textview中模拟一次触摸即可。

如果你想做一些模拟触摸,请检查一下。 https://github.com/HUYU2048/PTFakeTouch

© www.soinside.com 2019 - 2024. All rights reserved.