Ahmad F
Ahmad F

Reputation: 31665

How UITextInputAssistantItem works with custom inputView in iOS 13?

I have a view controller with one textfield, I am using inputAssistantItem of the textfield to a button for each group (leading and trailing) for the keyboard shortcuts bar, simply as:

class MyViewController: UIViewController {

    @IBOutlet weak var textField: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()

        setupKeypad()
    }

    private func setupKeypad() {
        let item: UITextInputAssistantItem = textField.inputAssistantItem
        let doneButton = UIButton()
        doneButton.setTitle("done", for: .normal)
        doneButton.addTarget(self, action: #selector(donePressed), for: .touchUpInside)

        let cancelButton = UIButton()
        cancelButton.setTitle("cancel", for: .normal)
        cancelButton.addTarget(self, action: #selector(cancelPressed), for: .touchUpInside)

        let rightBarButtonItem = UIBarButtonItem(customView: doneButton)
        let leftBarButtonItem = UIBarButtonItem(customView: cancelButton)

        let leftGroup = UIBarButtonItemGroup(barButtonItems: [leftBarButtonItem], representativeItem: nil)
        let rightGroup = UIBarButtonItemGroup(barButtonItems: [rightBarButtonItem], representativeItem: nil)

        item.leadingBarButtonGroups = [leftGroup]
        item.trailingBarButtonGroups = [rightGroup]
    }

    @objc func donePressed() {
        print(#function)
    }

    @objc func cancelPressed() {
        print(#function)
    }
}

Nothing special, just calling setupKeypad() in viewDidLoad() method to add the two buttons. It works fine as expected:

enter image description here

However, when attempting to add a custom inputView for the textfield, the shortcuts bar is not displayed anymore:

override func viewDidLoad() {
    super.viewDidLoad()

    // adding dummy custom view as inputView for the textField
    let customView = UIView(frame: CGRect(x: 0, y: 0, width: 300.0, height: 300.0))
    customView.backgroundColor = .orange
    textField.inputView = customView

    setupKeypad()
}

The result is:

enter image description here

I double-checked it on prior iOS versions (12.4-) and it works fine with custom inputViews. For example, the result for iOS 12.2 is:

enter image description here

How to resolve this issue?

Upvotes: 5

Views: 1095

Answers (0)

Related Questions