用户登录
用户注册

分享至

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

  • 作者: 六味地黄丸治肾亏不含糖
  • 来源: 51数据库
  • 2023-02-09

问题描述

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

My iPad app uses an external "device" that acts as a hardware keyboard. But, at some point in the settings, I need to input text and I can't use the "device" ("device" is not a keyboard). So, is there any way to force pop the soft keyboard even thought I have a hardware keyboard connected?

推荐答案

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

Yes. We've done this in a few of our apps for when the user has a Bluetooth scanner "keyboard" paired with the device. What you can do is make sure your textField has an inputAccessoryView and then force the frame of the inputAccessoryView yourself. This will cause the keyboard to display on screen.

我们在 AppDelegate 中添加了以下两个函数.'inputAccessoryView' 变量是我们在应用委托中声明的 UIView*:

We added the following two functions to our AppDelegate. The 'inputAccessoryView' variable is a UIView* we have declared in our app delegate:

//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 中,我们添加了这个通知观察者,这样我们就可以在文本字段开始编辑时收到一个事件

Then in our applicationDidFinishLaunching we added this notification observer so we would get an event anytime a text field began editing

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

希望有帮助!

软件
前端设计
程序设计
Java相关