How to move the entire view when the keyboard pops up? (fast)

The gray box at the bottom is the text view. When I click on the text view, the keyboard will pop up from the bottom. However, the pop-up keyboard has covered the text view.

When the keyboard pops up, what function should I add to move the entire view up?

To detect when the keyboard is displayed, you can listen to NSNotificationCenter

NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: “ keyboardWillHide:", name: UIKeyboardWillHideNotification, object: nil)

That will call keyboardWillShow and keyboardWillHide. Here, you can use UITextfield to complete the required operation

< pre>func keyboardWillShow(notification: NSNotification) {
if let userInfo = notification.userInfo {
if let keyboardSize = (userInfo[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
/ /use keyboardSize.height to determine the height of the keyboard and set the height of your textfield accordingly
}
}
}

func keyboardWillHide(notification: NSNotification) {
//pull everything down again
}

The gray box at the bottom is the text view. When I click on the text view, the keyboard will pop up from the bottom. However, the pop-up keyboard has covered the text view.

When When the keyboard pops up, what function should I add to move the entire view up?

To detect when the keyboard is displayed, you can listen to NSNotificationCenter

NSNotificationCenter.defaultCenter( ).addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: “keyboardWillHide:", name: UIKeyboardWillHideNotification, object: nil)

That will call keyboardWillShow and keyboardWillHide. Here, you can use UITextfield to complete the required operation

func keyboardWillShow(notification: NSNotification) {
if let userInfo = notification.userInfo {
if let keyboardSize = (userInfo[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
//use keyboardSize.height to determine the height of the keyboard and set the height of your textfield accordingly
}
}
}

func keyboardWillHide(notification: NSNotification) {
//pull everything down again< br />}

Leave a Comment

Your email address will not be published.