在iOS 8上获取键盘大小无法使用外部键盘 [英] Get keyboard size on iOS 8 not working with external keyboard

查看:110
本文介绍了在iOS 8上获取键盘大小无法使用外部键盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在正在看几个SO帖子,但似乎没有人帮助我。只需要设置 UITableView 的底部约束,以便键盘永远不会位于tableview之上。在iOS 8中似乎不可能。它在使用普通软键盘时起作用,但是当在模拟器中或使用真实硬件键盘移除它时,它仍然认为有一个软键盘。

Been looking at several SO post now, but none seem to help me. Just want a UITableViews bottom constraint to be set so that the keyboard never is on top of the tableview. Seems impossible in iOS 8. It work when using normal soft keyboard, but when removing it in simulator or using real hardware keyboard, it still thinks there is a soft keyboard.

代码:

var currentKeyboardHeight:CGFloat = 0.0

override public func viewDidLoad() {
    super.viewDidLoad()

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

deinit {
    NSNotificationCenter.defaultCenter().removeObserver(self)
}

func keyboardWillShow(notification: NSNotification) {
    if let userInfo = notification.userInfo {
        var keyboardRect = userInfo[UIKeyboardFrameEndUserInfoKey]!.CGRectValue()

        let windowRect = view.window!.convertRect(keyboardRect, fromWindow:nil)
        let viewRect = view.convertRect(windowRect, fromView:nil)

        let deltaHeight = viewRect.size.height - currentKeyboardHeight

        currentKeyboardHeight = viewRect.size.height
        tableViewBottomConstraint.constant = currentKeyboardHeight
        UIView.animateWithDuration(0.25, animations: { () -> Void in
            self.view.layoutIfNeeded()
        })

        println("show keyboard: \(currentKeyboardHeight)")
    }
}

func keyboardWillHide(notification: NSNotification) {
    if let userInfo = notification.userInfo {
        var keyboardRect = userInfo[UIKeyboardFrameEndUserInfoKey]!.CGRectValue()
        keyboardRect = self.view.convertRect(keyboardRect, fromView: nil)

        let deltaHeight = keyboardRect.size.height - currentKeyboardHeight
        currentKeyboardHeight = 0
        tableViewBottomConstraint.constant = currentKeyboardHeight

        UIView.animateWithDuration(0.25, animations: { () -> Void in
            self.view.layoutIfNeeded()
        })

        println("hideKeyboard \(currentKeyboardHeight)")

    }
}

何时在最后一个tableviewrow中选择 uitextview ,只打印这个:

When selecting a uitextview in the last tableviewrow only this is printed:

show keyboard: 260.0

这是错误的。如果那时你打开软键盘(cmd + shift + k):

which is Wrong. If then you turn on soft keyboard (cmd+shift+k):

show keyboard: 260.0
show keyboard: 297.0
show keyboard: 297.0

这是正确的。然后将其关闭(cmd + shift + k):

which is Correct. And then turn it off (cmd+shift+k):

hideKeyboard 0.0
show keyboard: 260.0
hideKeyboard 0.0

这是正确的

更新1:

根据此答案的评论( https://stackoverflow.com/a/2893327/511299 )使用 accessoryView <时, UIKeyboardWillShowNotification 将始终触发/ code>我是谁。

According to the comment on this answer (https://stackoverflow.com/a/2893327/511299) the UIKeyboardWillShowNotification will always fire when using a accessoryView which I am.

func setupKeyboardButtons()
{
    let numberToolbar = UIToolbar(frame: CGRectMake(0,0,320,50))
    numberToolbar.barStyle = UIBarStyle.Default

    let button1 = UIBarButtonItem(title: CANCEL_TEXT, style: UIBarButtonItemStyle.Plain, target: self, action: "keyboardCancelButtonTapped:")
    let button2 = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
    let button3 = UIBarButtonItem(title: SAVE_TEXT, style: UIBarButtonItemStyle.Plain, target: self, action: "keyboardDoneButtonTapped:")

    button1.tintColor = UIColor.darkGrayColor()

    numberToolbar.items = [button1, button2, button3]

    numberToolbar.sizeToFit()
    commentTextView.inputAccessoryView = numberToolbar
}


推荐答案

您可以检测外部键盘是否已连接,并通过执行类似操作将键盘高度设置为0.

You can detect if external keyboard is connected and and set keyboard's height to 0 by doing something like that.

Objective-C版本:

Objective-C version:

CGRect finalKeyboardFrame = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
finalKeyboardFrame = [self convertRect:finalKeyboardFrame fromView:self.window];

NSLog(@"origin.y: %f", finalKeyboardFrame.origin.y);

根据外接键盘的连接是否正确显示y坐标:

Shows the y coordinate properly depending if external keyboard is connected or not:


origin.y:258.000000(未连接)

origin.y: 258.000000 (not connected)

origin.y:474.000000(已连接)

origin.y: 474.000000 (connected)

这篇关于在iOS 8上获取键盘大小无法使用外部键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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