【问题标题】:Get keyboard size on iOS 8 not working with external keyboard在 iOS 8 上获取键盘大小不适用于外部键盘
【发布时间】:2015-07-08 23:12:07
【问题描述】:

现在一直在看几个 SO 帖子,但似乎没有一个对我有帮助。只想设置一个UITableViews 底部约束,以便键盘永远不会位于表格视图的顶部。在iOS 8中似乎不可能。使用普通软键盘时它可以工作,但是在模拟器中删除它或使用真正的硬件键盘时,它仍然认为有一个软键盘。

代码:

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 时,只会打印这个:

show keyboard: 260.0

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

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

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

hideKeyboard 0.0
show keyboard: 260.0
hideKeyboard 0.0

这是正确的

更新 1:

根据对此答案 (https://stackoverflow.com/a/2893327/511299) 的评论,UIKeyboardWillShowNotification 在使用 accessoryView 时总是会触发。

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
}

【问题讨论】:

    标签: ios swift ios8 keyboard uikeyboard


    【解决方案1】:

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

    Objective-C 版本:

    CGRect finalKeyboardFrame = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    finalKeyboardFrame = [self convertRect:finalKeyboardFrame fromView:self.window];
    
    NSLog(@"origin.y: %f", finalKeyboardFrame.origin.y);
    

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

    origin.y:258.000000(未连接)

    origin.y:474.000000(已连接)

    【讨论】:

    • 谢谢。使用外部键盘时,它们似乎有所不同。但是它们代表什么,我该如何设置我的底部约束?
    • 你可以。计算键盘的实际高度:self.frame.size.height - finalKeyboardFrame.origin.y
    • 谢谢,我自己弄明白了:)
    猜你喜欢
    • 2014-11-24
    • 1970-01-01
    • 1970-01-01
    • 2015-09-15
    • 2011-08-03
    • 1970-01-01
    • 2013-09-27
    • 2016-07-02
    • 2015-02-03
    相关资源
    最近更新 更多