gpt4 book ai didi

ios - 通知、键盘隐藏

转载 作者:行者123 更新时间:2023-11-30 12:36:40 26 4
gpt4 key购买 nike

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
keyboardUpNotification()
keyboardDownNotification()
}

func keyboardUpNotification() {
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
print("keyboard up notification registered")
}
func keyboardWillShow(_ notification: Notification) {
view.frame.origin.y = 0 - keyboardSize(notification)
print("keyboard will show method. \(keyboardSize(notification))")
}


func keyboardDownNotification() {
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
print("keyboard down notification registered")
}
func keyboardWillHide() {
print("keyboard will hide method")
view.frame.origin.y = 0
}

我添加了两个通知(键盘向上、向下)。我希望当我触摸返回按钮时键盘隐藏。但 KeyboardWillHide() 方法没有调用。我做错了什么或错过了什么?

最佳答案

第一件事

1.让你的ViewController确认textField委托(delegate)

class YourViewController: UIViewController, UITextFieldDelegate {

}

2.将 self 设置为 textField 委托(delegate)

override func viewDidLoad() {
super.viewDidLoad()
self.yourTextField.delegate = self
}

3.实现

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
self.yourTextField.resignFirstResponder()
return true
}

关于ios - 通知、键盘隐藏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42778662/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com