Xcode Constraint Break When Switching Between Text Fields Without Dismissing Keyboard

Viewed 887

I've been battling an issue in Xcode for a couple of days now and I can't figure out what's wrong. I've created a simple UIViewcontroller with three separate Textfields. When I run the app in the simulator or on a physical device, I get this warning in the console when I tap on the textfields. It happens only when I tap on the next textfield without dismissing the keyboard first. If I dismiss the keyboard after every textfield input, there are no debug console complaints. But when I don't the console returns the following on tapping the next textfield.

"[LayoutConstraints] Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it.

"<NSLayoutConstraint:0x281f15bd0 'assistantHeight' TUISystemInputAssistantView:0x14602a340.height == 55   (active)>",
"<NSLayoutConstraint:0x281f2de00 'assistantView.bottom' TUISystemInputAssistantView:0x14602a340.bottom == _UIKBCompatInputView:0x1460631d0.top   (active)>",
"<NSLayoutConstraint:0x281f2fcf0 'assistantView.top' V:|-(0)-[TUISystemInputAssistantView:0x14602a340]   (active, names: '|':UIInputSetHostView:0x143e5cd50 )>",
"<NSLayoutConstraint:0x281f17200 'inputView.top' V:|-(0)-[_UIKBCompatInputView:0x1460631d0]   (active, names: '|':UIInputSetHostView:0x143e5cd50 )>"

Will attempt to recover by breaking constraint Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in may also be helpful."

I have created a new, clean UIViewcontroller with nothing inside, just three simple textfields and I still experience this issue. I've done this, because in my original project, I had the textfields arranged inside a stackview and thought that might be causing the issue, but it clearly isn't.

Has anyone encountered this issue?

Thanks very much for your time and any help!

3 Answers

I believe that this is a bug in apple's implementation of the keyboard.

So the work around for this is set autocorrection type of text field to no

textField.autocorrectionType = .no

The layout constraint error message has nothing to do with your layout.

It's an internal issue (could be considered a bug) with the Input Assistant View - the auto-complete options shown above the keyboard.

If you use Debug View Hierarchy you can find the culprit listed in the error message: TUISystemInputAssistantView

You can ignore this error / warning message, as it is out of your hands.

Edit

The error / warning message shows:

"<NSLayoutConstraint:0x281f15bd0 'assistantHeight' TUISystemInputAssistantView:0x14602a340.height == 55   (active)>",

Here is what you can find using Debug View Hierarchy:

enter image description here

As you can see, the "offending element" is the TUISystemInputAssistantView which is part of iOS internal functionality.

You might be facing issue, while setting constraints, so I've done it, similar to you, and able to tap on the next textfield without dismissing the keyboard first.

I've tested below solution. You can try this solution.

I hope it will solve your issue.

class ViewController: UIViewController {

    let nameTextField: UITextField = {
        let textField = UITextField()
        textField.placeholder = "Enter your name here"
        textField.layer.borderWidth = 1
        textField.borderStyle = .roundedRect
        textField.translatesAutoresizingMaskIntoConstraints = false
        return textField
    }()

    let ageTextField: UITextField = {
        let textField = UITextField()
        textField.placeholder = "Enter your age here"
        textField.keyboardType = .decimalPad
        textField.layer.borderWidth = 1
        textField.borderStyle = .roundedRect
        textField.translatesAutoresizingMaskIntoConstraints = false
        return textField
    }()

    let cityTextField: UITextField = {
        let textField = UITextField()
        textField.placeholder = "Enter your city name here"
        textField.layer.borderWidth = 1
        textField.borderStyle = .roundedRect
        textField.translatesAutoresizingMaskIntoConstraints = false
        return textField
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.addSubview(nameTextField)
        self.view.addSubview(ageTextField)
        self.view.addSubview(cityTextField)

        NSLayoutConstraint.activate([

            //nameTextField
            self.nameTextField.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor, constant: 100),
            self.nameTextField.leadingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.leadingAnchor, constant: 16),
            self.nameTextField.trailingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.trailingAnchor, constant: -16),


            //ageTextField
            self.ageTextField.topAnchor.constraint(equalTo: self.nameTextField.bottomAnchor, constant: 16),
            self.ageTextField.leadingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.leadingAnchor, constant: 16),
            self.ageTextField.trailingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.trailingAnchor, constant: -16),

            //cityTextField
            self.cityTextField.topAnchor.constraint(equalTo: self.ageTextField.bottomAnchor, constant: 16),
            self.cityTextField.leadingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.leadingAnchor, constant: 16),
            self.cityTextField.trailingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.trailingAnchor, constant: -16),

            ])
    }
}
Related