如何获得键盘的高度?

Lac*_*tan 91 uikeyboard ios swift

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

小智 194

在Swift中:

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

像这样的东西:

斯威夫特2

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
Run Code Online (Sandbox Code Playgroud)

斯威夫特3

NotificationCenter.default.addObserver(
    self,
    selector: #selector(keyboardWillShow),
    name: NSNotification.Name.UIKeyboardWillShow,
    object: nil
)
Run Code Online (Sandbox Code Playgroud)

斯威夫特4

NotificationCenter.default.addObserver(
    self,
    selector: #selector(keyboardWillShow),
    name: UIResponder.keyboardWillShowNotification,
    object: nil
)
Run Code Online (Sandbox Code Playgroud)

然后你可以在这样的keyboardWillShow函数中访问高度:

斯威夫特2

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
}
Run Code Online (Sandbox Code Playgroud)

斯威夫特3

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

斯威夫特4

@objc func keyboardWillShow(_ notification: Notification) {
    if let keyboardFrame: NSValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue {
        let keyboardRectangle = keyboardFrame.cgRectValue
        let keyboardHeight = keyboardRectangle.height
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 是的,迅速3混乱了。每次给予不同的价值 (3认同)

fs_*_*gre 62

Swift 3.0和Swift 4.1

1-在viewWillAppear方法中注册通知:

NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: .UIKeyboardWillShow, object: nil)
Run Code Online (Sandbox Code Playgroud)

2-要求的方法:

@objc func keyboardWillShow(notification: NSNotification) {
    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
        let keyboardHeight = keyboardSize.height
        print(keyboardHeight)
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 您需要将"UIKeyboardFrameBeginUserInfoKey"更改为"UIKeyboardFrameEndUserInfoKey",因为在您的示例中,您通常可以获得零高度.详细信息:/sf/ask/3198276511/ (5认同)
  • 在`viewDidLoad`中注册通知不是一个好主意:你在哪里放置匹配的`removeObserver`调用,以便当这个VC不再显示时,它会停止收到通知?最好将通知的注册放在`viewWillAppear`中,然后将`removeObserver`调用放在`viewWillDisappear`中. (4认同)
  • 这样做,你也可以让`keyboardWillShow`参数为`Notification`类型,使其更符合Swift 3.0. (3认同)

Edu*_*ias 23

Swift 4和Constraints

在您的tableview中添加相对于底部安全区域的底部约束.在我的例子中,约束称为tableViewBottomLayoutConstraint.

@IBOutlet weak var tableViewBottomLayoutConstraint: NSLayoutConstraint!

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillAppear(notification:)), name: .UIKeyboardWillShow, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillDisappear(notification:)), name: .UIKeyboardWillHide, object: nil)
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)

    NotificationCenter.default.removeObserver(self, name: .UIKeyboardWillShow , object: nil)
    NotificationCenter.default.removeObserver(self, name: .UIKeyboardWillHide , object: nil)
}

@objc 
func keyboardWillAppear(notification: NSNotification?) {

    guard let keyboardFrame = notification?.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue else {
        return
    }

    let keyboardHeight: CGFloat
    if #available(iOS 11.0, *) {
        keyboardHeight = keyboardFrame.cgRectValue.height - self.view.safeAreaInsets.bottom
    } else {
        keyboardHeight = keyboardFrame.cgRectValue.height
    }

    tableViewBottomLayoutConstraint.constant = keyboardHeight
}

@objc 
func keyboardWillDisappear(notification: NSNotification?) {
    tableViewBottomLayoutConstraint.constant = 0.0
}
Run Code Online (Sandbox Code Playgroud)

  • 考虑到安全区域的任何这些问题的第一个答案.谢谢! (2认同)

Jes*_*sse 18

更新Swift 4.2

private func setUpObserver() {
    NotificationCenter.default.addObserver(self, selector: .keyboardWillShow, name: UIResponder.keyboardWillShowNotification, object: nil)
}
Run Code Online (Sandbox Code Playgroud)

选择器方法:

@objc fileprivate func keyboardWillShow(notification:NSNotification) {
    if let keyboardRectValue = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
        let keyboardHeight = keyboardRectValue.height
    }
}
Run Code Online (Sandbox Code Playgroud)

延期:

private extension Selector {
    static let keyboardWillShow = #selector(YourViewController.keyboardWillShow(notification:)) 
}
Run Code Online (Sandbox Code Playgroud)

更新Swift 3.0

private func setUpObserver() {
    NotificationCenter.default.addObserver(self, selector: .keyboardWillShow, name: .UIKeyboardWillShow, object: nil)
}
Run Code Online (Sandbox Code Playgroud)

选择器方法:

@objc fileprivate func keyboardWillShow(notification:NSNotification) {
    if let keyboardRectValue = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
        let keyboardHeight = keyboardRectValue.height
    }
}
Run Code Online (Sandbox Code Playgroud)

延期:

private extension Selector {
    static let keyboardWillShow = #selector(YourViewController.keyboardWillShow(notification:)) 
}
Run Code Online (Sandbox Code Playgroud)

小费

UIKeyboardDidShowNotification或UIKeyboardWillShowNotification可能会调用两次并获得不同的结果,本文解释了为什么调用两次.

在Swift 2.2中

Swift 2.2不赞成使用选择器的字符串,而是引入了新的语法:#selector.

就像是:

private func setUpObserver() {

    NSNotificationCenter.defaultCenter().addObserver(self, selector: .keyboardWillShow, name: UIKeyboardWillShowNotification, object: nil)
}
Run Code Online (Sandbox Code Playgroud)

选择器方法:

@objc private 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
    editorBottomCT.constant = keyboardHeight
}
Run Code Online (Sandbox Code Playgroud)

延期:

    private extension Selector {

    static let keyboardWillShow = #selector(YourViewController.keyboardWillShow(_:)) 
}
Run Code Online (Sandbox Code Playgroud)

  • 不要忘记删除deinit中的观察者,例如:NSNotificationCenter.defaultCenter().removeObserver(self) (3认同)

Viv*_*vek 12

斯威夫特 5

override func viewDidLoad() {
    //  Registering for keyboard notification.
    NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
}


/*  UIKeyboardWillShowNotification. */
    @objc internal func keyboardWillShow(_ notification : Notification?) -> Void {
        
        var _kbSize:CGSize!
        
        if let info = notification?.userInfo {

            let frameEndUserInfoKey = UIResponder.keyboardFrameEndUserInfoKey
            
            //  Getting UIKeyboardSize.
            if let kbFrame = info[frameEndUserInfoKey] as? CGRect {
                
                let screenSize = UIScreen.main.bounds
                
                //Calculating actual keyboard displayed size, keyboard frame may be different when hardware keyboard is attached (Bug ID: #469) (Bug ID: #381)
                let intersectRect = kbFrame.intersection(screenSize)
                
                if intersectRect.isNull {
                    _kbSize = CGSize(width: screenSize.size.width, height: 0)
                } else {
                    _kbSize = intersectRect.size
                }
                print("Your Keyboard Size \(_kbSize)")
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

  • 这是一个很好的答案。当软件键盘被隐藏时,这个结果是正确的。 (3认同)

Ale*_*ign 11

更短的版本:

func keyboardWillShow(notification: NSNotification) {

        if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
            let keyboardHeight = keyboardSize.height
        }
}
Run Code Online (Sandbox Code Playgroud)


ZAF*_*007 5

斯威夫特4

最简单的方法

override func viewDidLoad() {
      super.viewDidLoad()
      NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: .UIKeyboardWillShow, object: nil)
}

func keyboardWillShow(notification: NSNotification) {  

      if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
             let keyboardHeight : Int = Int(keyboardSize.height)
             print("keyboardHeight",keyboardHeight) 
      }

}
Run Code Online (Sandbox Code Playgroud)