如何将textview的自定义输入视图设置为与iOS中默认键盘相同的高度?

时间:2018-03-22 02:34:33

标签: ios swift ios11 custom-keyboard

我希望textView的自定义inputView的高度与默认键盘的高度相同。我尝试了很多,包括:

self.textView.inputView?.autoresizingMask = .flexibleHeight

我找不到解决方案。它总是比默认键盘少。 (编辑:此问题仅适用于iPhone X)

我的代码:

class ViewController: UIViewController {

    let textView = UITextView()
    let button = UIButton()

    var keyboardView = UIView()

    override func viewDidLoad() {
        super.viewDidLoad()
        button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
        textView.backgroundColor = .lightGray
        button.backgroundColor = .red
        self.view.addSubview(textView)
        self.view.addSubview(button)
    }

    @objc func buttonAction() {
        if self.textView.inputView == nil {
            self.textView.inputView = keyboardView
            self.textView.inputView?.autoresizingMask = .flexibleHeight
            self.textView.reloadInputViews()
        } else {
            self.textView.inputView = nil
            self.textView.reloadInputViews()
        }
    }

    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        textView.frame = CGRect(x: 0, y: 50, width: self.view.frame.size.width - 50, height: 50)
        button.frame = CGRect(x: self.view.frame.size.width - 50, y: 50, width: 50, height: 50)
    }
}

0 个答案:

没有答案