当键盘出现时,我使用以下代码向上移动视图。
这工作正常,但我有一个问题,如果我在文本字段上tapp并直接点击下一个文本字段,它会将我的视图向上移动两次。
如何让它只移动一次视图?
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name: UIKeyboardWillHideNotification, object: nil)
func keyboardWillShow(notification: NSNotification) {
print(self.view.frame.origin.y)
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
self.view.frame.origin.y -= keyboardSize.height
}
print(self.view.frame.origin.y)
}
func keyboardWillHide(notification: NSNotification) {
print("is dissapaering now")
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
self.view.frame.origin.y += keyboardSize.height
}
}
答案 0 :(得分:1)
听起来你应该在再次显示之前检查键盘是否已经显示。创建一个跟踪状态的新Bool变量,如果Bool值为false,则仅移动视图。