gpt4 book ai didi

objective-c - 使用 inputAccessoryView 关闭键盘

转载 作者:行者123 更新时间:2023-11-30 10:15:44 24 4
gpt4 key购买 nike

TextView 停靠在底部(就像消息应用程序一样)。但是,当用户点击 textView 外部时,键盘不会关闭。

import UIKit
class CommentsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet var commentBar: UIView!

@IBOutlet var commentTextField: UITextField!

override var inputAccessoryView: UIView {
return commentBar
}

override func canBecomeFirstResponder() -> Bool {
commentBar.removeFromSuperview()
return true
}

func textFieldShouldReturn(textField: UITextField!) -> Bool {
self.view.endEditing(true);
return false;
}

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
self.view.endEditing(true);
commentTextField.resignFirstResponder()
}

最佳答案

无需在 canBecomeFirstResponder 方法中删除commentBar。这将在每次键盘退出时调用。

尝试这样的事情。

class ViewController: UIViewController {
@IBOutlet var commentBar: UIView!
@IBOutlet var commentTextField: UITextField!

override func viewDidLoad() {
super.viewDidLoad()
commentBar.removeFromSuperview()
}
override var inputAccessoryView: UIView {
return commentBar
}

override func canBecomeFirstResponder() -> Bool {
return true
}

func textFieldShouldReturn(textField: UITextField!) -> Bool {
self.view.endEditing(true);
return false;
}

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
self.view.endEditing(true);
commentTextField.resignFirstResponder()
}

}

关于objective-c - 使用 inputAccessoryView 关闭键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30091761/

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