1
我有一个视图,它在键盘出现时调整约束条件。所以我有键盘出现和消失时的通知。旋转后接收到两个不同高度的键盘
上述行为发生在键盘已经显示并且我旋转屏幕时。随后发生的下一个动作:
- UIKeyboardWillHideNotification叫
- UIKeyboardWillShowNotification称为(与旧的高度键盘)
- UIKeyboardWillShowNotification称为(与键盘的新高度)
所以, updateView函数接收第一个高度,然后接收不同的高度。这导致视图的奇怪移动调整了两倍的值。
override func viewDidLoad() {
super.viewDidLoad()
// Creates notification when keyboard appears and disappears
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name: UIKeyboardWillHideNotification, object: nil)
}
deinit {
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: nil)
}
func keyboardWillShow(notification: NSNotification) {
self.adjustingHeight(true, notification: notification)
}
func keyboardWillHide(notification: NSNotification) {
self.adjustingHeight(false, notification: notification)
}
private func adjustingHeight(show: Bool, notification: NSNotification) {
// Gets notification information in an dictionary
var userInfo = notification.userInfo!
// From information dictionary gets keyboard’s size
let keyboardFrame:CGRect = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).CGRectValue()
// Gets the time required for keyboard pop up animation
let animationDurarion = userInfo[UIKeyboardAnimationDurationUserInfoKey] as! NSTimeInterval
// Animation moving constraint at same speed of moving keyboard & change bottom constraint accordingly.
if show {
self.bottomConstraint.constant = (CGRectGetHeight(keyboardFrame) + self.bottomConstraintConstantDefault/2)
} else {
self.bottomConstraint.constant = self.bottomConstraintConstantDefault
}
UIView.animateWithDuration(animationDurarion) {
self.view.layoutIfNeeded()
}
self.hideLogoIfSmall()
}
不是很确定,但也许可以添加另一个观察“UIKeyboardWillChangeFrameNotification”并处理其选择器中的高度? – Allen
@艾伦我认为我会处于同样的境地。我将不得不在UIKeyboardWillShowNotification中处理高度,并且UIKeyboardWillChangeFrameNotification也会以不同的高度被调用两次。 – angeldev