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

tag*_*yro 22 iphone keyboard cocoa-touch uikit ipad

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

Bri*_*ins 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);
}
Run Code Online (Sandbox Code Playgroud)

然后在我们的applicationDidFinishLaunching中添加了这个通知观察器,这样我们就可以在文本字段开始编辑的任何时候获得一个事件

    //Setup the textFieldNotifications
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldBegan:) name:UITextFieldTextDidBeginEditingNotification object:nil];
Run Code Online (Sandbox Code Playgroud)

希望有所帮助!