如果屏幕旋转,UIKeyboardWillShowNotification 会调用两次
UIKeyboardWillShowNotification called twice if screen rotates
我在键盘出现和消失时创建通知。
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)
}
override func viewWillDisappear(animated: Bool) {
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 != self.bottomConstraintConstantDefault) {
if show {
self.bottomConstraint.constant = (CGRectGetHeight(keyboardFrame) + self.bottomConstraintConstantDefault / 2)
} else {
self.bottomConstraint.constant = self.bottomConstraintConstantDefault
}
UIView.animateWithDuration(animationDurarion) {
self.view.layoutIfNeeded()
}
}
self.hideLogoIfSmall()
}
当键盘已经显示并且我旋转屏幕时会出现奇怪的行为。然后发生下一个动作:
- 已调用 UIKeyboardWillHideNotification
- 调用了 UIKeyboardWillShowNotification(使用键盘的旧高度)
- 已调用 UIKeyboardWillShowNotification(使用新的键盘高度)
结果是我的视图没有正确更新,因为第一次调用 UIKeyboardWillShowNotification 我收到的键盘高度与第二次不同。
我终于找到了解决办法。当我得到 keyboardFrame
时,我正在使用 UIKeyboardFrameBeginUserInfoKey
动画开始前的 returns 键盘帧。正确的做法是 UIKeyboardFrameEndUserInfoKey
动画完成后 returns 键盘的帧。
let keyboardFrame: CGRect = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
现在 UIKeyboardWillShowNotification
被调用了两次,但是 returns 两次调用的键盘高度相同。
我在键盘出现和消失时创建通知。
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)
}
override func viewWillDisappear(animated: Bool) {
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 != self.bottomConstraintConstantDefault) {
if show {
self.bottomConstraint.constant = (CGRectGetHeight(keyboardFrame) + self.bottomConstraintConstantDefault / 2)
} else {
self.bottomConstraint.constant = self.bottomConstraintConstantDefault
}
UIView.animateWithDuration(animationDurarion) {
self.view.layoutIfNeeded()
}
}
self.hideLogoIfSmall()
}
当键盘已经显示并且我旋转屏幕时会出现奇怪的行为。然后发生下一个动作:
- 已调用 UIKeyboardWillHideNotification
- 调用了 UIKeyboardWillShowNotification(使用键盘的旧高度)
- 已调用 UIKeyboardWillShowNotification(使用新的键盘高度)
结果是我的视图没有正确更新,因为第一次调用 UIKeyboardWillShowNotification 我收到的键盘高度与第二次不同。
我终于找到了解决办法。当我得到 keyboardFrame
时,我正在使用 UIKeyboardFrameBeginUserInfoKey
动画开始前的 returns 键盘帧。正确的做法是 UIKeyboardFrameEndUserInfoKey
动画完成后 returns 键盘的帧。
let keyboardFrame: CGRect = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
现在 UIKeyboardWillShowNotification
被调用了两次,但是 returns 两次调用的键盘高度相同。