这里有一个简单的例子。我在FirstResponder
中呼叫viewDidLoad
。但配件视图仅在点击屏幕后显示。为什么它从一开始就没有显示出来?
class TestViewController: MainPageViewController {
private let accessoryView = UIView() //TextInputView() // MessageInputAccessoryView()
override var inputAccessoryView: UIView {
return accessoryView
}
override var canBecomeFirstResponder: Bool { return true }
override func viewDidLoad() {
super.viewDidLoad()
accessoryView.backgroundColor = .red
accessoryView.frame = CGRect(x: 0, y: 0, width: 300, height: 50)
self.becomeFirstResponder()
// Do any additional setup after loading the view.
let tap = UITapGestureRecognizer(target: self, action: #selector(tappo))
self.view.isUserInteractionEnabled = true
self.view.addGestureRecognizer(tap)
tappo()
}
func tappo() {
self.becomeFirstResponder()
}
}
答案 0 :(得分:4)
viewWillAppear
是放置becomesFirstResponder
的更好地方。试试吧。
答案 1 :(得分:2)
所以有些东西让我的第一个响应者辞职(因为我使用的是UIPageViewController
)。所以我已将此添加到我的UIViewControler
:
override var canResignFirstResponder: Bool { return false }
那就是它。干杯!