swiftautolayoutuitextfielduitoolbarinputaccessoryview

Layout Constraint Errors with simple UIToolar for keyboard inputAccessoryView


I was getting a lot of layout constraint errors so I created a new project and simply have a UIToolbar and a UITextField to try and troubleshoot the problem. Even with a clean project, I still get strange errors even though I'm not overriding any constraints for the .inputAccessoryView.

The code is as follows (no storyboard content):

import UIKit

class ViewController: UIViewController {

    var aTextField = UITextField()

    override func viewDidLoad() {
        super.viewDidLoad()

        let aKeyboardToolBar = UIToolbar()

        // add buttons to the toolbar
        let prevButton = UIBarButtonItem(title: "PREV", style: .done, target: self, action: #selector(prevButtonTapped))
        let nextButton = UIBarButtonItem(title: "NEXT", style: .done, target: self, action: #selector(nextButtonTapped))
        let doneButton = UIBarButtonItem(title: "DONE", style: .done, target: self, action: #selector(doneButtonTapped))
        let flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil)

        aKeyboardToolBar.setItems([prevButton, nextButton, flexibleSpace, doneButton], animated: true)
        aKeyboardToolBar.sizeToFit()

        // Add first textfield
        view.addSubview(aTextField)
        aTextField.backgroundColor = .orange
        aTextField.placeholder = "Blank TextField"
        aTextField.inputAccessoryView = aKeyboardToolBar

        // Layout first text field
        aTextField.translatesAutoresizingMaskIntoConstraints = false
        aTextField.topAnchor.constraint(equalTo: view.topAnchor, constant: 150).isActive = true
        aTextField.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 20).isActive = true
        aTextField.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -20).isActive = true
    }



    @objc func prevButtonTapped() {
        print("prevButtonTapped")
    }



    @objc func nextButtonTapped() {
        print("nextButtonTapped")
    }



    @objc func doneButtonTapped() {
        view.endEditing(true)
    }

}

The errors (KeyboardShifter is the name of the project):

2020-01-12 11:41:16.300180+1100 KeyboardShifter[1304:1170583] [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. 
    (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x283f240a0 h=--& v=--& _UIToolbarContentView:0x10125fed0.width == 0   (active)>",
    "<NSLayoutConstraint:0x283f45d60 H:|-(0)-[_UIButtonBarStackView:0x10120fba0]   (active, names: '|':_UIToolbarContentView:0x10125fed0 )>",
    "<NSLayoutConstraint:0x283f45db0 _UIButtonBarStackView:0x10120fba0.trailing == _UIToolbarContentView:0x10125fed0.trailing   (active)>",
    "<NSLayoutConstraint:0x283f2ab70 'TB_Leading_Leading' H:|-(8)-[_UIModernBarButton:0x1011645b0'NEXT']   (active, names: '|':_UIButtonBarButton:0x1011641d0 )>",
    "<NSLayoutConstraint:0x283f2abc0 'TB_Trailing_Trailing' H:[_UIModernBarButton:0x1011645b0'NEXT']-(8)-|   (active, names: '|':_UIButtonBarButton:0x1011641d0 )>",
    "<NSLayoutConstraint:0x283f2b5c0 'UISV-canvas-connection' UILayoutGuide:0x282568460'UIViewLayoutMarginsGuide'.leading == _UIButtonBarButton:0x10115d760.leading   (active)>",
    "<NSLayoutConstraint:0x283f2b610 'UISV-canvas-connection' UILayoutGuide:0x282568460'UIViewLayoutMarginsGuide'.trailing == _UIButtonBarButton:0x1011650e0.trailing   (active)>",
    "<NSLayoutConstraint:0x283f2b840 'UISV-spacing' H:[_UIButtonBarButton:0x10115d760]-(0)-[UIView:0x101164060]   (active)>",
    "<NSLayoutConstraint:0x283f2b890 'UISV-spacing' H:[UIView:0x101164060]-(0)-[_UIButtonBarButton:0x1011641d0]   (active)>",
    "<NSLayoutConstraint:0x283f2b8e0 'UISV-spacing' H:[_UIButtonBarButton:0x1011641d0]-(0)-[UIView:0x101164f70]   (active)>",
    "<NSLayoutConstraint:0x283f2b930 'UISV-spacing' H:[UIView:0x101164f70]-(0)-[_UIButtonBarButton:0x1011650e0]   (active)>",
    "<NSLayoutConstraint:0x283f45bd0 'UIView-leftMargin-guide-constraint' H:|-(0)-[UILayoutGuide:0x282568460'UIViewLayoutMarginsGuide'](LTR)   (active, names: '|':_UIButtonBarStackView:0x10120fba0 )>",
    "<NSLayoutConstraint:0x283f45c70 'UIView-rightMargin-guide-constraint' H:[UILayoutGuide:0x282568460'UIViewLayoutMarginsGuide']-(0)-|(LTR)   (active, names: '|':_UIButtonBarStackView:0x10120fba0 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x283f2abc0 'TB_Trailing_Trailing' H:[_UIModernBarButton:0x1011645b0'NEXT']-(8)-|   (active, names: '|':_UIButtonBarButton:0x1011641d0 )>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
2020-01-12 11:41:16.301336+1100 KeyboardShifter[1304:1170583] [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. 
    (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x283f240a0 h=--& v=--& _UIToolbarContentView:0x10125fed0.width == 0   (active)>",
    "<NSLayoutConstraint:0x283f45d60 H:|-(0)-[_UIButtonBarStackView:0x10120fba0]   (active, names: '|':_UIToolbarContentView:0x10125fed0 )>",
    "<NSLayoutConstraint:0x283f45db0 _UIButtonBarStackView:0x10120fba0.trailing == _UIToolbarContentView:0x10125fed0.trailing   (active)>",
    "<NSLayoutConstraint:0x283f2a490 'TB_Leading_Leading' H:|-(0)-[_UIModernBarButton:0x10115e6d0'PREV']   (active, names: '|':_UIButtonBarButton:0x10115d760 )>",
    "<NSLayoutConstraint:0x283f2a4e0 'TB_Trailing_Trailing' H:[_UIModernBarButton:0x10115e6d0'PREV']-(8)-|   (active, names: '|':_UIButtonBarButton:0x10115d760 )>",
    "<NSLayoutConstraint:0x283f2b5c0 'UISV-canvas-connection' UILayoutGuide:0x282568460'UIViewLayoutMarginsGuide'.leading == _UIButtonBarButton:0x10115d760.leading   (active)>",
    "<NSLayoutConstraint:0x283f2b610 'UISV-canvas-connection' UILayoutGuide:0x282568460'UIViewLayoutMarginsGuide'.trailing == _UIButtonBarButton:0x1011650e0.trailing   (active)>",
    "<NSLayoutConstraint:0x283f2b840 'UISV-spacing' H:[_UIButtonBarButton:0x10115d760]-(0)-[UIView:0x101164060]   (active)>",
    "<NSLayoutConstraint:0x283f2b890 'UISV-spacing' H:[UIView:0x101164060]-(0)-[_UIButtonBarButton:0x1011641d0]   (active)>",
    "<NSLayoutConstraint:0x283f2b8e0 'UISV-spacing' H:[_UIButtonBarButton:0x1011641d0]-(0)-[UIView:0x101164f70]   (active)>",
    "<NSLayoutConstraint:0x283f2b930 'UISV-spacing' H:[UIView:0x101164f70]-(0)-[_UIButtonBarButton:0x1011650e0]   (active)>",
    "<NSLayoutConstraint:0x283f45bd0 'UIView-leftMargin-guide-constraint' H:|-(0)-[UILayoutGuide:0x282568460'UIViewLayoutMarginsGuide'](LTR)   (active, names: '|':_UIButtonBarStackView:0x10120fba0 )>",
    "<NSLayoutConstraint:0x283f45c70 'UIView-rightMargin-guide-constraint' H:[UILayoutGuide:0x282568460'UIViewLayoutMarginsGuide']-(0)-|(LTR)   (active, names: '|':_UIButtonBarStackView:0x10120fba0 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x283f2a4e0 'TB_Trailing_Trailing' H:[_UIModernBarButton:0x10115e6d0'PREV']-(8)-|   (active, names: '|':_UIButtonBarButton:0x10115d760 )>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
2020-01-12 11:41:16.302024+1100 KeyboardShifter[1304:1170583] [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. 
    (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x283f240a0 h=--& v=--& _UIToolbarContentView:0x10125fed0.width == 0   (active)>",
    "<NSLayoutConstraint:0x283f45d60 H:|-(0)-[_UIButtonBarStackView:0x10120fba0]   (active, names: '|':_UIToolbarContentView:0x10125fed0 )>",
    "<NSLayoutConstraint:0x283f45db0 _UIButtonBarStackView:0x10120fba0.trailing == _UIToolbarContentView:0x10125fed0.trailing   (active)>",
    "<NSLayoutConstraint:0x283f2b200 'TB_Leading_Leading' H:|-(8)-[_UIModernBarButton:0x1011652c0'DONE']   (active, names: '|':_UIButtonBarButton:0x1011650e0 )>",
    "<NSLayoutConstraint:0x283f2b250 'TB_Trailing_Trailing' H:[_UIModernBarButton:0x1011652c0'DONE']-(0)-|   (active, names: '|':_UIButtonBarButton:0x1011650e0 )>",
    "<NSLayoutConstraint:0x283f2b5c0 'UISV-canvas-connection' UILayoutGuide:0x282568460'UIViewLayoutMarginsGuide'.leading == _UIButtonBarButton:0x10115d760.leading   (active)>",
    "<NSLayoutConstraint:0x283f2b610 'UISV-canvas-connection' UILayoutGuide:0x282568460'UIViewLayoutMarginsGuide'.trailing == _UIButtonBarButton:0x1011650e0.trailing   (active)>",
    "<NSLayoutConstraint:0x283f2b840 'UISV-spacing' H:[_UIButtonBarButton:0x10115d760]-(0)-[UIView:0x101164060]   (active)>",
    "<NSLayoutConstraint:0x283f2b890 'UISV-spacing' H:[UIView:0x101164060]-(0)-[_UIButtonBarButton:0x1011641d0]   (active)>",
    "<NSLayoutConstraint:0x283f2b8e0 'UISV-spacing' H:[_UIButtonBarButton:0x1011641d0]-(0)-[UIView:0x101164f70]   (active)>",
    "<NSLayoutConstraint:0x283f2b930 'UISV-spacing' H:[UIView:0x101164f70]-(0)-[_UIButtonBarButton:0x1011650e0]   (active)>",
    "<NSLayoutConstraint:0x283f45bd0 'UIView-leftMargin-guide-constraint' H:|-(0)-[UILayoutGuide:0x282568460'UIViewLayoutMarginsGuide'](LTR)   (active, names: '|':_UIButtonBarStackView:0x10120fba0 )>",
    "<NSLayoutConstraint:0x283f45c70 'UIView-rightMargin-guide-constraint' H:[UILayoutGuide:0x282568460'UIViewLayoutMarginsGuide']-(0)-|(LTR)   (active, names: '|':_UIButtonBarStackView:0x10120fba0 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x283f2b250 'TB_Trailing_Trailing' H:[_UIModernBarButton:0x1011652c0'DONE']-(0)-|   (active, names: '|':_UIButtonBarButton:0x1011650e0 )>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
2020-01-12 11:41:16.302742+1100 KeyboardShifter[1304:1170583] [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. 
    (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x283f24140 h=--& v=--& _UIToolbarContentView:0x10125fed0.height == 0   (active)>",
    "<NSLayoutConstraint:0x283f45e00 V:|-(0)-[_UIButtonBarStackView:0x10120fba0]   (active, names: '|':_UIToolbarContentView:0x10125fed0 )>",
    "<NSLayoutConstraint:0x283f45e50 _UIButtonBarStackView:0x10120fba0.bottom == _UIToolbarContentView:0x10125fed0.bottom   (active)>",
    "<NSLayoutConstraint:0x283f250e0 UIButtonLabel:0x101163420'PREV'.centerY == _UIModernBarButton:0x10115e6d0'PREV'.centerY + 1.5   (active)>",
    "<NSLayoutConstraint:0x283f2a670 'TB_Baseline_Baseline' _UIModernBarButton:0x10115e6d0'PREV'.lastBaseline == UILayoutGuide:0x28257c620'UIViewLayoutMarginsGuide'.bottom   (active)>",
    "<NSLayoutConstraint:0x283f2a6c0 'TB_Top_Top' V:|-(>=0)-[_UIModernBarButton:0x10115e6d0'PREV']   (active, names: '|':_UIButtonBarButton:0x10115d760 )>",
    "<NSLayoutConstraint:0x283f2a800 'UIButtonBar.maximumAlignmentSize' _UIButtonBarButton:0x10115d760.height == UILayoutGuide:0x282568460'UIViewLayoutMarginsGuide'.height   (active)>",
    "<NSLayoutConstraint:0x283f45c20 'UIView-bottomMargin-guide-constraint' V:[UILayoutGuide:0x282568460'UIViewLayoutMarginsGuide']-(0)-|   (active, names: '|':_UIButtonBarStackView:0x10120fba0 )>",
    "<NSLayoutConstraint:0x283f2a5d0 'UIView-bottomMargin-guide-constraint' V:[UILayoutGuide:0x28257c620'UIViewLayoutMarginsGuide']-(11)-|   (active, names: '|':_UIButtonBarButton:0x10115d760 )>",
    "<NSLayoutConstraint:0x283f45b80 'UIView-topMargin-guide-constraint' V:|-(0)-[UILayoutGuide:0x282568460'UIViewLayoutMarginsGuide']   (active, names: '|':_UIButtonBarStackView:0x10120fba0 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x283f250e0 UIButtonLabel:0x101163420'PREV'.centerY == _UIModernBarButton:0x10115e6d0'PREV'.centerY + 1.5   (active)>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
2020-01-12 11:41:16.303504+1100 KeyboardShifter[1304:1170583] [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. 
    (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x283f24140 h=--& v=--& _UIToolbarContentView:0x10125fed0.height == 0   (active)>",
    "<NSLayoutConstraint:0x283f45e00 V:|-(0)-[_UIButtonBarStackView:0x10120fba0]   (active, names: '|':_UIToolbarContentView:0x10125fed0 )>",
    "<NSLayoutConstraint:0x283f45e50 _UIButtonBarStackView:0x10120fba0.bottom == _UIToolbarContentView:0x10125fed0.bottom   (active)>",
    "<NSLayoutConstraint:0x283f25810 UIButtonLabel:0x101164880'NEXT'.centerY == _UIModernBarButton:0x1011645b0'NEXT'.centerY + 1.5   (active)>",
    "<NSLayoutConstraint:0x283f2ad50 'TB_Baseline_Baseline' _UIModernBarButton:0x1011645b0'NEXT'.lastBaseline == UILayoutGuide:0x28257c700'UIViewLayoutMarginsGuide'.bottom   (active)>",
    "<NSLayoutConstraint:0x283f2ada0 'TB_Top_Top' V:|-(>=0)-[_UIModernBarButton:0x1011645b0'NEXT']   (active, names: '|':_UIButtonBarButton:0x1011641d0 )>",
    "<NSLayoutConstraint:0x283f2aee0 'UIButtonBar.maximumAlignmentSize' _UIButtonBarButton:0x1011641d0.height == UILayoutGuide:0x282568460'UIViewLayoutMarginsGuide'.height   (active)>",
    "<NSLayoutConstraint:0x283f45c20 'UIView-bottomMargin-guide-constraint' V:[UILayoutGuide:0x282568460'UIViewLayoutMarginsGuide']-(0)-|   (active, names: '|':_UIButtonBarStackView:0x10120fba0 )>",
    "<NSLayoutConstraint:0x283f2acb0 'UIView-bottomMargin-guide-constraint' V:[UILayoutGuide:0x28257c700'UIViewLayoutMarginsGuide']-(11)-|   (active, names: '|':_UIButtonBarButton:0x1011641d0 )>",
    "<NSLayoutConstraint:0x283f45b80 'UIView-topMargin-guide-constraint' V:|-(0)-[UILayoutGuide:0x282568460'UIViewLayoutMarginsGuide']   (active, names: '|':_UIButtonBarStackView:0x10120fba0 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x283f25810 UIButtonLabel:0x101164880'NEXT'.centerY == _UIModernBarButton:0x1011645b0'NEXT'.centerY + 1.5   (active)>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
2020-01-12 11:41:16.304192+1100 KeyboardShifter[1304:1170583] [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. 
    (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x283f24140 h=--& v=--& _UIToolbarContentView:0x10125fed0.height == 0   (active)>",
    "<NSLayoutConstraint:0x283f45e00 V:|-(0)-[_UIButtonBarStackView:0x10120fba0]   (active, names: '|':_UIToolbarContentView:0x10125fed0 )>",
    "<NSLayoutConstraint:0x283f45e50 _UIButtonBarStackView:0x10120fba0.bottom == _UIToolbarContentView:0x10125fed0.bottom   (active)>",
    "<NSLayoutConstraint:0x283f25f40 UIButtonLabel:0x101165590'DONE'.centerY == _UIModernBarButton:0x1011652c0'DONE'.centerY + 1.5   (active)>",
    "<NSLayoutConstraint:0x283f2b3e0 'TB_Baseline_Baseline' _UIModernBarButton:0x1011652c0'DONE'.lastBaseline == UILayoutGuide:0x28257c7e0'UIViewLayoutMarginsGuide'.bottom   (active)>",
    "<NSLayoutConstraint:0x283f2b430 'TB_Top_Top' V:|-(>=0)-[_UIModernBarButton:0x1011652c0'DONE']   (active, names: '|':_UIButtonBarButton:0x1011650e0 )>",
    "<NSLayoutConstraint:0x283f2b570 'UIButtonBar.maximumAlignmentSize' _UIButtonBarButton:0x1011650e0.height == UILayoutGuide:0x282568460'UIViewLayoutMarginsGuide'.height   (active)>",
    "<NSLayoutConstraint:0x283f45c20 'UIView-bottomMargin-guide-constraint' V:[UILayoutGuide:0x282568460'UIViewLayoutMarginsGuide']-(0)-|   (active, names: '|':_UIButtonBarStackView:0x10120fba0 )>",
    "<NSLayoutConstraint:0x283f2b340 'UIView-bottomMargin-guide-constraint' V:[UILayoutGuide:0x28257c7e0'UIViewLayoutMarginsGuide']-(11)-|   (active, names: '|':_UIButtonBarButton:0x1011650e0 )>",
    "<NSLayoutConstraint:0x283f45b80 'UIView-topMargin-guide-constraint' V:|-(0)-[UILayoutGuide:0x282568460'UIViewLayoutMarginsGuide']   (active, names: '|':_UIButtonBarStackView:0x10120fba0 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x283f25f40 UIButtonLabel:0x101165590'DONE'.centerY == _UIModernBarButton:0x1011652c0'DONE'.centerY + 1.5   (active)>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.

Any ideas?


Solution

  • Change

    let aKeyboardToolBar = UIToolbar()
    

    to

    let aKeyboardToolBar = UIToolbar(frame:CGRect(x:0, y:0, width:100, height:100))
    

    That way it has some size to work with as you are giving it content.