在iOS中启动应用程序时如何获取键盘的高度

时间:2019-02-27 07:55:50

标签: ios swift iphone uikeyboard

我创建了一个聊天板应用程序。但是在加载任何功能之前,我需要在应用程序启动时设置键盘高度。我尝试了以下一个...

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

然后我在我的代码中添加了此函数...它将返回键盘高度,但在加载textFieldDidBeginEditing方法之后

@objc func keyboardWillShow(notification: Notification) {
    let userInfo:NSDictionary = notification.userInfo! as NSDictionary
    let keyboardFrame:NSValue = userInfo.value(forKey: UIKeyboardFrameEndUserInfoKey) as! NSValue
    let keyboardRectangle = keyboardFrame.cgRectValue
    keyboardHeight = keyboardRectangle.height
}

textFieldDidBeginEditing方法看起来像这样

func textFieldDidBeginEditing(_ textField: UITextField) {
    UIView.animate(withDuration: 0.50) {

        self.heightConstraint.constant = self.textFieldBody.bounds.height + self.keyboardHeight;
        self.view.layoutIfNeeded();
    }
}

但这不会为我提供起点的值!

0 个答案:

没有答案