Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to simultaneously satisfy constraints when UIToolbarContentView is present

I have a custom button on top of a datepicker view. When the datepicker is active I get the "Unable to simultaneously satisfy constraints." The error has popped up After I updated to Xcode 11,

This is my code:

let toolbar = UIToolbar()
let flexBarButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: nil, action: nil)
let done = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: #selector(viewTapped))
toolbar.sizeToFit()
toolbar.setItems([flexBarButton,done], animated: false)
dateInputTextField.inputAccessoryView = toolbar

This is the error:

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:0x7f85a6fe4230 h=--& v=--& _UIToolbarContentView:0x7f85a6d1b8a0.width == 0   (active)>",
    "<NSLayoutConstraint:0x7f85a6c2c220 H:|-(0)-[_UIButtonBarStackView:0x7f85a6d1c0c0]   (active, names: '|':_UIToolbarContentView:0x7f85a6d1b8a0 )>",
    "<NSLayoutConstraint:0x7f85a6c2e5e0 _UIButtonBarStackView:0x7f85a6d1c0c0.trailing == _UIToolbarContentView:0x7f85a6d1b8a0.trailing   (active)>",
    "<NSLayoutConstraint:0x7f85a6c2f920 'TB_Leading_Leading' H:|-(16)-[_UIModernBarButton:0x7f85a6edf790'Done']   (active, names: '|':_UIButtonBarButton:0x7f85a6ed7060 )>",
    "<NSLayoutConstraint:0x7f85a6c2d690 'TB_Trailing_Trailing' H:[_UIModernBarButton:0x7f85a6edf790'Done']-(16)-|   (active, names: '|':_UIButtonBarButton:0x7f85a6ed7060 )>",
    "<NSLayoutConstraint:0x7f85a6f11060 'UISV-canvas-connection' UILayoutGuide:0x7f85a6c22b40'UIViewLayoutMarginsGuide'.leading == UIView:0x7f85a6ed6cd0.leading   (active)>",
    "<NSLayoutConstraint:0x7f85a6f25390 'UISV-canvas-connection' UILayoutGuide:0x7f85a6c22b40'UIViewLayoutMarginsGuide'.trailing == _UIButtonBarButton:0x7f85a6ed7060.trailing   (active)>",
    "<NSLayoutConstraint:0x7f85a6f256e0 'UISV-spacing' H:[UIView:0x7f85a6ed6cd0]-(0)-[_UIButtonBarButton:0x7f85a6ed7060]   (active)>",
    "<NSLayoutConstraint:0x7f85a6c24880 'UIView-leftMargin-guide-constraint' H:|-(0)-[UILayoutGuide:0x7f85a6c22b40'UIViewLayoutMarginsGuide'](LTR)   (active, names: '|':_UIButtonBarStackView:0x7f85a6d1c0c0 )>",
    "<NSLayoutConstraint:0x7f85a6c2c9c0 'UIView-rightMargin-guide-constraint' H:[UILayoutGuide:0x7f85a6c22b40'UIViewLayoutMarginsGuide']-(0)-|(LTR)   (active, names: '|':_UIButtonBarStackView:0x7f85a6d1c0c0 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7f85a6c2d690 'TB_Trailing_Trailing' H:[_UIModernBarButton:0x7f85a6edf790'Done']-(16)-|   (active, names: '|':_UIButtonBarButton:0x7f85a6ed7060 )>

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 to fix the error?

like image 754
Stormwright Avatar asked Oct 23 '19 20:10

Stormwright


2 Answers

Swift 5.2

The constraint error seems to occur when the width of the UIToolbar is set to the width of the screen. The height seems to be irrelevant.

I have silenced the warning by setting a width wide enough to accept the UIBarButtonItem(s). Making the width too small also invokes the error.

let toolBar = UIToolbar(frame: CGRect(origin: .zero, size: CGSize(width: 100, height: 44.0)))
like image 125
rbaldwin Avatar answered Oct 11 '22 11:10

rbaldwin


In my case this was resolved by specifying a valid frame when instantiating the UIToolbar.
UIToolbar(frame: ..) instead of just UIToolbar()

For example:

UIToolbar(frame: CGRect(origin: CGPoint.zero, size: CGSize(width: _viewSizeWidth, height: CGFloat(44))))

where _viewSizeWidth was calculated elsewhere based on the window size.

like image 17
jk7 Avatar answered Oct 11 '22 09:10

jk7