我有一个UITabBarController
,其标签为UINavigationControllers
。
当键盘打开时,视图底部与键盘之间存在奇怪的间隙。我检查了所有约束,看起来很好。
我有一个不同的ViewController,其UITextView
并没有显示它之间的差距。
这是我的代码,它将键盘高度添加到最底部的约束,以便它正确滚动并且不会隐藏任何内容:
func setupKeyboardObserver() {
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWasShown:"), name:UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name:UIKeyboardWillHideNotification, object: nil)
}
func keyboardWasShown(notification: NSNotification) {
let info = notification.userInfo!
let keyboardFrame: CGRect = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
UIView.animateWithDuration(0.1, animations: { () -> Void in
self.bottomConstraint.constant = keyboardFrame.size.height
})
}
func keyboardWillHide(notification: NSNotification) {
var info = notification.userInfo!
var keyboardFrame: CGRect = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
UIView.animateWithDuration(0.1, animations: { () -> Void in
self.bottomConstraint.constant = 0
})
}