override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: .UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: .UIKeyboardWillHide, object: nil)
print("addd observer")
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
NotificationCenter.default.removeObserver(self, name: .UIKeyboardWillShow, object: nil)
NotificationCenter.default.removeObserver(self, name: .UIKeyboardWillHide, object: nil)
}
func keyboardWillShow(_ sender: Notification) {
let keyboardSize = (sender.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue
print("show")
if(activeTectField != Lable_title && keyboard_status == false)
{
boxview.frame.origin.y -= (keyboardSize?.height)!
keyboard_status = true
}
}
func keyboardWillHide(_ sender: Notification) {
let keyboardSize = (sender.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue
if(keyboard_status == true){
boxview.frame.origin.y = 0
boxview.layoutIfNeeded()
keyboard_status = false
}
}
我添加了观察者,当键盘弹出时移动视图,它首先工作,但点击几下后,它不起作用。在viewWillAppear()
中,已添加观察者,但键盘未弹出,keyboardwillshow()
也未被触发。我怎么解决这个问题?欢迎任何建议
答案 0 :(得分:1)
为了使用目标/选择器模式,这些方法必须与Objective-C兼容。
使用hystrix-core
修饰符前面的键盘方法。
@objc