1. Set the function for monitoring keyboard events:
override func viewWillAppear (animated: Bool) {
NSNotificationCenter.defaultCenter (). AddObserver (self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter (). AddObserver (self, selector: "keyboardWillHide:", name: UIKeyboardWillHideNotification, object: nil)
}
2. Handle pop-up events:
func keyboardWillShow (notification: NSNotification) {if let keyboardSize = (notification.userInfo? [UIKeyboardFrameBeginUserInfoKey] as? NSValue) ?. CGRectValue () {
// let contentInsets = UIEdgeInsets (top: 0, left: 0, bottom: keyboardSize.height, right: 0)
// let keyboardheight = keyboardSize.height as CGFloat
// let frame = self.nameInput.frame
// frame.origin.y = frame.origin.y-keyboardheight
// var offset = 156 as CGFloat
var width = self.view.frame.size.width;
var height = self.view.frame.size.height;
let rect = CGRectMake (0.0, -156, width, height);
self.view.frame = rect
}
}
3. Handle the recovery event:
func keyboardWillHide (notification: NSNotification) {
self.view.addSubview (logoArea)
var width = self.view.frame.size.width;
var height = self.view.frame.size.height;
let rect = CGRectMake (0.0, 0, width, height);
self.view.frame = rect
}
swift pop-up keyboard view automatically rises