我正在使用下面的观察器确定我的键盘高度。 然后,我使用此 keyboardHeight 来调整UIView(附加图像)的底部约束:
@IBOutlet weak var postViewBottomContraint: NSLayoutConstraint!
override func viewWillAppear(_ animated: Bool) {
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: .UIKeyboardWillShow, object: nil)
}
和方法:
@objc func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
keyboardHeight = keyboardSize.height
print("Keyboard Height is:",keyboardHeight)
}
}
,这是我更改高度的地方:
func startEditing() {
UIView.animate(withDuration: 0.5) {
self.postViewBottomContraint.constant = self.keyboardHeight
print ("Bottom constraint is:",self.postViewBottomContraint.constant.description)
self.postTextView.textColor = UIColor.lightGray
self.view.layoutIfNeeded()
}
}
即使 keyboardHeight 和 bottomConstraint 相同(在我的情况下为253.0),它也会在键盘和UIView之间添加一个空格,是否会添加一些额外内容那不可见或还有其他事情吗?