iOS支持外接键盘而不显示一个 [英] iOS Support External Keyboard Without Displaying One

查看:236
本文介绍了iOS支持外接键盘而不显示一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出一个UIViewController,我只想接收来自外部键盘的文本输入.考虑UIKeyCommand,但要考虑任何字符(不仅仅是修改"的字符).

Given a UIViewController, I would like to receive text input only from the external keyboard. Think UIKeyCommand but for any character (not just 'modified' ones).

但是,当我尝试使用UIKeyInput来实现该功能时,如果没有连接外部键盘,iOS迫切希望显示一个键盘.

However, when I try to implement that using UIKeyInput, it seems that iOS desperately wants to display a keyboard if there is no external one connected.

有什么办法可以避免这种情况?具体来说,是否只有在连接一个键盘的情况下,才可以选择接收来自键盘的击键?

Is there any way to circumvent that? Specifically, to have the options to receive keystrokes from the keyboard if, and only if, one is connected?

推荐答案

在摆弄了一个小时的iPad之后,我终于迅速找到了一个很好的解决方案.其他方法较弱或使用第三方软件.即使在iPad上使用外部键盘时,也会触发UIKeyboardWillShowNotification的原因是存在快捷键.为了禁用快捷栏,请执行以下操作:

After fiddling with a iPad for an hour, I finally have a good solution for this in swift. The other methods are weak or use 3rd party software. The reason why UIKeyboardWillShowNotification is getting fired even when an external keyboard is being used for an iPad is the shortcut bar existing. In order to disable the shortcut bar, do this:

    let item : UITextInputAssistantItem = textField.inputAssistantItem
    item.leadingBarButtonGroups = []
    item.trailingBarButtonGroups = []

这涵盖了您所需要的大多数情况,但是如果有人在某些使用点插入键盘,则仍会触发UIKeyboardWillShowNotification.如果您有屏幕调整功能,那么您将无法承受任何情况.另外,出于某些原因,您可能需要快捷方式栏.无论您的需求是什么,这都涵盖了使用外接键盘的所有情况:

This covers most cases of what you need, but UIKeyboardWillShowNotification can still be fired if someone plugs their keyboard in at certain points of use. If you have the screen adjust, you can't afford any case for the user to experience this. Plus, you might want the shortcut bar for some reason. Regardless of what your desires are, this covers all cases of an external keyboard being used:

添加到viewDidAppear

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(LoginViewController.keyboardWillShow), name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(LoginViewController.keyboardWillHide), name: UIKeyboardWillHideNotification, object: nil)

每当您离开视图时,请将其添加到任何让您离开的视图中

whenever you leave the view add this to anything that makes you leave

NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: nil)

还要将其添加到deinit{}方法中.

also add it to the deinit{} method to be thourough.

现在使用以下功能:

func keyboardWillShow(notification: NSNotification) {
    //   This code is an alternate way of checking for keyboard
    var userInfo: [NSObject: AnyObject] = notification.userInfo!
    let firstFrame = userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue
    let secondFrame = userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue
    let firstRect = firstFrame.CGRectValue()
    let secondRect = secondFrame.CGRectValue()
    let diff = abs(firstRect.origin.y - secondRect.origin.y)
    let isFirstBigger = firstRect.origin.y > secondRect.origin.y
    if firstRect != secondRect && diff != 55 {
        if !isFirstBigger {
            //animateViewToDefaultPosition()
        } else {
            //animateViewToPositionWhenKeyboardActive()
        }
    }
}

func keyboardWillHide() {
    //animateViewToDefaultPosition()
}

55是快捷栏的高度.如果您没有该功能,可以将其删除. !isFirstBigger用于检查何时在文本字段编辑期间将键盘解开并挂回.在该检查过程中,diff != 55也是很重要的,因为当您不想为屏幕设置动画时,会出现快捷方式栏.

The 55 is the height of the shortcut bar. You can remove it's functionality if you don't have one. The !isFirstBigger is used to check for when they unhook the keyboard and hook it back in during text field editing. It is also important that diff != 55 during that check because with a shortcut bar this would be the case when you did not want to animate the screen.

到目前为止,这是我检查堆栈溢出后所见过的最好的方法.如果有人发现该功能中的错误,请告诉我,但我相信它将解决烦人的快捷键外部键盘问题.我希望这可以帮助其他所有人对此感到困惑!

This is by far the best method I have seen after scouring Stack Overflow. If anyone finds a bug in the functionality, let me know, but I am confident it will take care of the pesky shortcut bar external keyboard issues. I hope this helps everyone else out there confused by all this!

这篇关于iOS支持外接键盘而不显示一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆