1

I want the height of custom inputView of the textView to be same height as that of default keyboard. I tried a lot, including:

self.textView.inputView?.autoresizingMask = .flexibleHeight

I couldn't find a solution. It's always coming less than that of default keyboard. (EDIT: This issue exists only for iPhone X)

My code:

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)
    }
}
Daniel James
  • 381
  • 1
  • 5
  • 15

0 Answers0