gpt4 book ai didi

ios - 无法检测到键盘消失?

转载 作者:行者123 更新时间:2023-11-28 10:26:50 24 4
gpt4 key购买 nike

我正在尝试检测键盘何时消失,以便降低文本字段。我该如何修改我的代码来执行此操作?

检测变化的方法:

    extension NotificationsViewController {

@objc func keyboardWillChange(notification: Notification) {
print("something djkshbfkhsalk")

guard let keyboardRect = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else { return }

if notification.name == UIResponder.keyboardWillShowNotification || notification.name == UIResponder.keyboardWillChangeFrameNotification {
messegeBox.frame.origin.y = (messegeBox.frame.origin.y-(keyboardRect.height/2))+5
} else {
messegeBox.frame.origin.y = messegeBox.frame.origin.y+(keyboardRect.height/2)-5
}

}

@objc func keyboardWillShow(notification: Notification) {
}

@objc func keyboardWillHide(notification: NSNotification) {
print("fdhsaflkjhdsajkfhdsajkhfjlk")
}

@objc func keyboardWillDisapear(notification: NSNotification) {
}

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
print("fdsafdsafdsafdsafdsaf")
msgTextField.resignFirstResponder()
return true
}

func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
msgTextField.resignFirstResponder()
}

}

设置第一个发生在 viewdidload 中,第二个是它自己的方法:

        //Listen for keyboard events
NotificationCenter.default.addObserver(self, selector: #selector(NotificationsViewController.keyboardWillChange(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(NotificationsViewController.keyboardWillChange(notification:)), name: UIResponder.keyboardWillHideNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(NotificationsViewController.keyboardWillChange(notification:)), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)

}

deinit {
//Stop listening for keyboard hide/show events
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillHideNotification, object: nil)
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
}

感谢您的帮助

最佳答案

您可以将这些更新为方法

NotificationCenter.default.addObserver(self, selector: #selector(NotificationsViewController.keyboardWillChange(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(NotificationsViewController.keyboardWillChange(notification:)), name: UIResponder.keyboardWillHideNotification, object: nil)

  NotificationCenter.default.addObserver(self, selector: #selector(NotificationsViewController. keyboardWillShow(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(NotificationsViewController. keyboardWillHide(notification:)), name: UIResponder.keyboardWillHideNotification, object: nil)

然后你会得到你写的现有方法中的事件

     @objc func keyboardWillShow(notification: Notification) {
//Called before keyboard shows
//Get the keyboard size like this
if let keyboardSize = (notification.userInfo? [UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
}
}

@objc func keyboardWillHide(notification: NSNotification) {
//Called before keyboard hides
}

使用这些方法你可以改变框架或滚动

关于ios - 无法检测到键盘消失?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58849370/

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