不从viewDidLoad显示的firstFirstResponder

时间:2017-04-07 13:34:33

标签: ios swift viewdidload becomefirstresponder

这里有一个简单的例子。我在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()
    }


}

2 个答案:

答案 0 :(得分:4)

viewWillAppear是放置becomesFirstResponder的更好地方。试试吧。

答案 1 :(得分:2)

所以有些东西让我的第一个响应者辞职(因为我使用的是UIPageViewController)。所以我已将此添加到我的UIViewControler

override var canResignFirstResponder: Bool { return false }

那就是它。干杯!