I learned from this existing question to get the func "keyboardwillshow" move up the entire view by the same height of the keyboard, in order to avoid the textfields/textviews at the bottom being covered by the keyboard.
However, if I tap two or more textfields in a row, the "keyboardwillshow" func will run by the same amount of tap, moving further up the view and eventually off the screen, leaving only black emptiness.
override func viewDidLoad() {
super.viewDidLoad()
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) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
self.view.frame.origin.y -= keyboardSize.height
}
}
func keyboardWillHide(notification: NSNotification) {
self.view.frame.origin.y = 0
}
Someone said in the answers that we can implement a boolean to detect whether the keyboard is already appeared or not, so the func will only work once.
Can somebody show me how to do this boolean? Thanks!