【发布时间】:2018-04-20 06:49:29
【问题描述】:
当它出现时,我需要在键盘上移动一个文本字段。
我正在使用以下代码:
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(self.keyBoardWillShow(_:)), name: .UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.keyBoardWillHide(_:)), name: .UIKeyboardWillHide, object: nil)
}
然后:
@objc func keyBoardWillShow(_ notification: NSNotification) {
let userInfo:NSDictionary = notification.userInfo! as NSDictionary
let keyboardFrame:NSValue = userInfo.value(forKey: UIKeyboardFrameEndUserInfoKey) as! NSValue
let keyboardRectangle = keyboardFrame.cgRectValue
let keyboardHeight = keyboardRectangle.height
UIView.animate(withDuration: 0.4) {
self.commentViewBottomConstraint.constant = keyboardHeight
self.view.layoutIfNeeded()
}
}
@objc func keyBoardWillHide(_ notification: NSNotification) {
UIView.animate(withDuration: 0.4) {
self.commentViewBottomConstraint.constant = 0
self.view.layoutIfNeeded()
}
}
问题是键盘高度似乎不正确。事实上,视图的底部并没有与键盘对齐。视图和键盘之间有一个空格。
老实说,我不明白我做错了什么......
感谢您的帮助!
【问题讨论】: