如何获得键盘的高度? [英] How to get height of Keyboard?

查看:325
本文介绍了如何获得键盘的高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不同iOS设备上键盘的高度不同。有没有人知道如何以编程方式获得设备键盘的高度?

The height of a keyboard on varying iOS devices is different. Does anybody know how I can get height of a device's keyboard programmatically?

推荐答案

在Swift中:

您可以通过订阅 UIKeyboardWillShowNotification 通知来获得键盘高度。 (假设你想知道它显示之前的高度)。

You can get the keyboard height by subscribing to the UIKeyboardWillShowNotification notification. (Assuming you want to know what the height will be before it's shown).

这样的事情:

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



Swift 3



Swift 3

NotificationCenter.default.addObserver(
    self,
    selector: #selector(keyboardWillShow),
    name: NSNotification.Name.UIKeyboardWillShow,
    object: nil
)

然后你可以在某些函数中访问高度:

Then you can access the height in some function like this:

func keyboardWillShow(notification:NSNotification) {
    let userInfo:NSDictionary = notification.userInfo!
    let keyboardFrame:NSValue = userInfo.valueForKey(UIKeyboardFrameEndUserInfoKey) as! NSValue
    let keyboardRectangle = keyboardFrame.CGRectValue()
    let keyboardHeight = keyboardRectangle.height
}



Swift 3



Swift 3

@objc func keyboardWillShow(_ notification: Notification) {
    if let keyboardFrame: NSValue = notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue {
        let keyboardRectangle = keyboardFrame.cgRectValue
        let keyboardHeight = keyboardRectangle.height
    }
}

这篇关于如何获得键盘的高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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