BecomeFirstResponder如何才能带上自定义的inputAccessoryView
how canBecomeFirstResponder bring custom inputAccessoryView
我正在尝试使自定义 inputAccessoryView
包含 UITextField
并将其停靠在底部,就像聊天应用程序一样
但如果我不使用 canBecomeFirstResponder = true
inputAccessoryView
是隐藏的(?)
这是我的代码
class MainVC: UITableViewController {
lazy var inputTextFieldContainer: UIView = {
// custom inputAccessoryView
}()
override func viewDidLoad(){
super.viewDidLoad()
}
override var inputAccessoryView: UIView? {
get{
return containerView
}
}
override var canBecomeFirstResponder: Bool {
get {
return true
}
}
}
如何canBecomeFirstResponder
带来自定义inputAccessoryView
我在 Apple 文档上阅读了有关 UIResponder 和响应链的信息
但我无法将这个概念与这个问题相匹配。
他们说 UIResponder 将处理事件,我通过 canBecomeFirstResponder = true
让我的 MainVC 成为第一响应者
显示 和 inputAccessoryView
但在这种情况下,具体事件是什么
由于您的代码继承自 UITableViewController,因此这里有一个完整的示例:
从定义附属视图开始。由于您以聊天应用为例,它可能是一个文本字段和一个发送按钮:
class SendMessageView: UIView {
private let textField = UITextField()
private let sendButton = UIButton()
init() {
super.init(frame: CGRect.zero)
self.setup()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) not implemented")
}
private func setup() {
sendButton.setTitle("Send", for: UIControlState.normal)
sendButton.setTitleColor(UIColor.blue, for: UIControlState.normal)
self.addSubview(textField)
self.addSubview(sendButton)
self.backgroundColor = UIColor.groupTableViewBackground
textField.backgroundColor = UIColor.white
self.autoresizingMask = .flexibleHeight
textField.translatesAutoresizingMaskIntoConstraints = false
sendButton.translatesAutoresizingMaskIntoConstraints = false
textField.topAnchor.constraint(equalTo: self.topAnchor, constant: 8).isActive = true
textField.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 16).isActive = true
sendButton.leadingAnchor.constraint(equalTo: textField.trailingAnchor, constant: 8).isActive = true
sendButton.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -16).isActive = true
sendButton.centerYAnchor.constraint(equalTo: textField.centerYAnchor).isActive = true
}
override var intrinsicContentSize: CGSize {
let contentHeight = self.textField.intrinsicContentSize.height + 16
return CGSize(width: UIScreen.main.bounds.width, height: contentHeight)
}
}
接下来您需要一个使用我们的辅助视图的自定义 UITableView:
class CustomTableView: UITableView {
private let sendMessageView = SendMessageView()
override var canBecomeFirstResponder: Bool {
return true
}
override var inputAccessoryView: UIView? {
return self.sendMessageView
}
}
终于可以使用此自定义 table 视图定义 TableViewController:
class TableViewController: UITableViewController {
override func loadView() {
self.tableView = CustomTableView()
self.view = self.tableView
}
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.becomeFirstResponder()
}
...
结果如下所示:
[
我正在尝试使自定义 inputAccessoryView
包含 UITextField
并将其停靠在底部,就像聊天应用程序一样
但如果我不使用 canBecomeFirstResponder = true
inputAccessoryView
是隐藏的(?)
这是我的代码
class MainVC: UITableViewController {
lazy var inputTextFieldContainer: UIView = {
// custom inputAccessoryView
}()
override func viewDidLoad(){
super.viewDidLoad()
}
override var inputAccessoryView: UIView? {
get{
return containerView
}
}
override var canBecomeFirstResponder: Bool {
get {
return true
}
}
}
如何canBecomeFirstResponder
带来自定义inputAccessoryView
我在 Apple 文档上阅读了有关 UIResponder 和响应链的信息
但我无法将这个概念与这个问题相匹配。
他们说 UIResponder 将处理事件,我通过 canBecomeFirstResponder = true
和 inputAccessoryView
但在这种情况下,具体事件是什么
由于您的代码继承自 UITableViewController,因此这里有一个完整的示例:
从定义附属视图开始。由于您以聊天应用为例,它可能是一个文本字段和一个发送按钮:
class SendMessageView: UIView {
private let textField = UITextField()
private let sendButton = UIButton()
init() {
super.init(frame: CGRect.zero)
self.setup()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) not implemented")
}
private func setup() {
sendButton.setTitle("Send", for: UIControlState.normal)
sendButton.setTitleColor(UIColor.blue, for: UIControlState.normal)
self.addSubview(textField)
self.addSubview(sendButton)
self.backgroundColor = UIColor.groupTableViewBackground
textField.backgroundColor = UIColor.white
self.autoresizingMask = .flexibleHeight
textField.translatesAutoresizingMaskIntoConstraints = false
sendButton.translatesAutoresizingMaskIntoConstraints = false
textField.topAnchor.constraint(equalTo: self.topAnchor, constant: 8).isActive = true
textField.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 16).isActive = true
sendButton.leadingAnchor.constraint(equalTo: textField.trailingAnchor, constant: 8).isActive = true
sendButton.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -16).isActive = true
sendButton.centerYAnchor.constraint(equalTo: textField.centerYAnchor).isActive = true
}
override var intrinsicContentSize: CGSize {
let contentHeight = self.textField.intrinsicContentSize.height + 16
return CGSize(width: UIScreen.main.bounds.width, height: contentHeight)
}
}
接下来您需要一个使用我们的辅助视图的自定义 UITableView:
class CustomTableView: UITableView {
private let sendMessageView = SendMessageView()
override var canBecomeFirstResponder: Bool {
return true
}
override var inputAccessoryView: UIView? {
return self.sendMessageView
}
}
终于可以使用此自定义 table 视图定义 TableViewController:
class TableViewController: UITableViewController {
override func loadView() {
self.tableView = CustomTableView()
self.view = self.tableView
}
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.becomeFirstResponder()
}
...
结果如下所示:
[