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