becomeFirstResponder 未从 viewDidLoad 显示

becomeFirstResponder not showing from viewDidLoad

这里我举个简单的例子。我正在 viewDidLoad 中呼叫 FirstResponder。但是附件视图只有在点击屏幕后才会出现。为什么一开始不显示?

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()
    }


}

viewWillAppear 更适合放置 becomesFirstResponder。试试看。

所以有些事情让我的第一响应者辞职了(因为我正在使用 UIPageViewController)。所以我已将此添加到我的 UIViewControler 中:

override var canResignFirstResponder: Bool { return false }

就是这样。干杯!