我在键盘上有一种奇怪的行为。但仅限于其中一个视图控制器。我有另一个带有UITextField的视图控制器,并且其中一个工作符号(一旦我按下返回按钮就消失了)。奇怪的是,一个甚至根本没有设置。它只是对viewdidload和viewwilldissapear上的resignfirstresponder的第一反应。
然而,这个键盘,VIDEO OF NASTY KEYBOARD是一个令人讨厌的鼻屎。
注意:在视频中,我执行以下操作:
*点击UITextfield
*键盘出现(查看向上移动)
*我键入lol然后按发送(没有任何反应)
*我开始在屏幕周围疯狂地敲击(除了UITextField)
*键盘消失
以下是此视图的代码:
class ChatScreenVC: UIViewController, UITextFieldDelegate {
var gameDelegate: GameManagerDelegate?
var chatLogString: String = ""
var offsetY:CGFloat = 0
@IBOutlet weak var chatView: UIView!
@IBOutlet weak var closeBtn: UIButton!
@IBOutlet weak var titleBtn: UILabel!
@IBOutlet weak var chatLog: UITextView!
@IBOutlet weak var chatInput: UITextField!
@IBOutlet weak var sendBtn: UIButton!
@IBOutlet weak var viewCenterConstraint: NSLayoutConstraint!
@IBAction func inputTapped(_ sender: Any) {
chatInput.becomeFirstResponder()
}
@IBAction func closeTapped(_ sender: Any) {
self.dismiss(animated: true, completion: nil)
}
@IBAction func sendTapped(_ sender: Any) {
if chatInput.text != nil && chatInput.text != "" && chatInput.text != " " {
gameDelegate?.sendChat(message: chatInput.text!)
}
chatInput.text = ""
chatInput.resignFirstResponder()
self.view.endEditing(true)
}
@objc func keyboardWillShow(_ notification: Notification) {
if let keyboardFrame: NSValue = notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue {
let keyboardRectangle = keyboardFrame.cgRectValue
let keyboardHeight = keyboardRectangle.height
viewCenterConstraint.constant = -keyboardHeight
}
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
print("THIS IS HAPPENING!")
chatInput.resignFirstResponder()
self.view.endEditing(true)
return true
}
@objc func keyboardWillHide(_ notification: Notification) {
if let keyboardFrame: NSValue = notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue {
viewCenterConstraint.constant = 0
}
}
@objc func reloadData() {
chatLogString = (gameDelegate?.updateChatString())!
chatLog.text = chatLogString
}
override func viewDidLoad() {
super.viewDidLoad()
chatLog.text = chatLogString
chatLog.isEditable = false
chatInput.autocorrectionType = .no
// Do any additional setup after loading the view.
}
override func viewWillAppear(_ animated: Bool) {
setupView()
animateView()
}
override func viewWillDisappear(_ animated: Bool) {
chatInput.resignFirstResponder()
}
override func viewDidAppear(_ animated: Bool) {
NotificationCenter.default.addObserver(self, selector: #selector(self.reloadData), name: Notification.Name(rawValue: SMACK_TALK), object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
override func viewDidDisappear(_ animated: Bool) {
NotificationCenter.default.removeObserver(self, name: Notification.Name(rawValue: SMACK_TALK), object: nil)
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil)
}
}
在您去之前告诉我"不要同时使用resignfirstresponder和enditing(true)"我得清楚的是,我单独和共同尝试过,但仍然没有得到任何回应。
答案 0 :(得分:0)
您可以在sendChat()
之前退出键盘。我认为这是同步调用,它会阻止主线程有时这就是为什么会发生这种奇怪的行为。
chatInput.resignFirstResponder()
。
@IBAction func sendTapped(_ sender: Any) {
if chatInput.text != nil && chatInput.text != "" && chatInput.text != " " {
chatInput.resignFirstResponder()
self.view.endEditing(true)
gameDelegate?.sendChat(message: chatInput.text!)
}
}