[英]Moving a View up or Down when a Keyboard is Shown
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
})
}
}
It doesnt work. 它不起作用。 it keeps on showing an error "type 'ViewController' has no member 'keyboardWillShow' Did you mean 'keyboardWillHide enter image description here 它不断显示错误“类型'ViewController'没有成员'keyboardWillShow'您是说'keyboardWillHide” 在这里输入图片描述
There is a couple of problems with your code. 您的代码有两个问题。 First the methods keyboardWillShow and keyboardWillHide should be exposed to Objective-c, second, your open/close curly brakets are messed up. 首先,应该将KeyboardWillShow和keyboardWillHide方法暴露给Objective-c,其次,打开/关闭卷曲表象弄乱了。
I made some modifications in the above code, try it like this: 我在上面的代码中做了一些修改,像这样尝试:
@objc 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
}
}
}
@objc func keyboardWillShow(_ sender: NSNotification) {
if let keyboardSize = (sender.userInfo?[UIKeyboardFrameBeginUserInfoKey]! as? NSValue)?.cgRectValue,
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
})
}
}
}
}
You may also want to ditch this altogeter and use IHKeyboardAvoiding component to solve the issue of keyboard hiding your fields. 您可能还希望放弃此Altogeter,并使用IHKeyboardAvoiding组件来解决键盘隐藏字段的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.