【发布时间】:2018-12-16 08:22:47
【问题描述】:
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillShow(_:)), name:NSNotification.Name.UIKeyboardWillShow, object: self.view.window)
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillHide(_:)), name:NSNotification.Name.UIKeyboardWillHide, object: self.view.window)
}
override func viewWillDisappear(_ animated: Bool) {
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: self.view.window)
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: self.view.window)
}
func keyboardWillHide(_ sender: Notification) {
if let keyboardSize = (sender.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
if self.view.frame.origin.y != 0{
self.view.frame.origin.y += keyboardSize.height
}
func keyboardWillShow(_ sender: NSNotification) {
if let keyboardSize = (sender.userInfo?[UIKeyboardFrameBeginUserInfoKey]! as? NSValue)?.cgRectValue {
if let offset = (sender.userInfo?[UIKeyboardFrameEndUserInfoKey]! as? NSValue)?.cgRectValue {
if self.view.frame.origin.y == 0{
self.view.frame.origin.y -= keyboardSize.height
if keyboardSize.height == offset.height {
if self.view.frame.origin.y == 0 {
UIView.animate(withDuration: 0.15, animations: {
self.view.frame.origin.y -= keyboardSize.height
})
}
}
else {
UIView.animate(withDuration: 0.15, animations: {
self.view.frame.origin.y += keyboardSize.height - offset.height
})
}
}
它不起作用。它一直显示错误“类型'ViewController'没有成员'keyboardWillShow'你的意思是'keyboardWillHideenter image description here
【问题讨论】: