UIToolbar with UIBarButtonItem LayoutConstraint issue
Asked Answered
B

2

23

I'm creating a UIToolbar with UIBarButtonItem in it programatically. I'm not using .xib or storyboard for this ViewController. Here's the code of how I created it.

NSMutableArray *items = [[NSMutableArray alloc] init];

UIBarButtonItem *buttonItem;
buttonItem = [[UIBarButtonItem alloc ] initWithTitle: [Language get:@"Home" alter:nil]
                                               style: UIBarButtonItemStyleBordered
                                              target: self
                                              action: @selector(viewChangeTo)];
[items addObject:buttonItem];

toolbar = [[UIToolbar alloc] init];
toolbar.frame = CGRectMake(0, 0, 1024, 44);
[toolbar setItems:items animated:NO];
[self.view addSubview:toolbar];

Each time the view get loaded I'm getting the LayoutConstraints error.

[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:0x6000023de350 H:|-(20)-[_UIModernBarButton:0x7fed8c463e00'Home']   (active, names: '|':_UIButtonBarButton:0x7fed8c463bb0 )>",
"<NSLayoutConstraint:0x6000023de3a0 H:[_UIModernBarButton:0x7fed8c463e00'Home']-(20)-|   (active, names: '|':_UIButtonBarButton:0x7fed8c463bb0 )>",
"<NSLayoutConstraint:0x6000023dff70 '_UITemporaryLayoutWidth' _UIButtonBarButton:0x7fed8c463bb0.width == 0   (active)>

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 <UIKitCore/UIView.h> may also be helpful.
2019-01-21 13:14:24.777354+0800 M[50994:10130357] [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:0x6000023de530 _UIModernBarButton:0x7fed8c463e00'Home'.lastBaseline == UILayoutGuide:0x6000039eef40'UIViewLayoutMarginsGuide'.bottom   (active)>",
    "<NSLayoutConstraint:0x6000023de580 V:|-(>=0)-[_UIModernBarButton:0x7fed8c463e00'Home']   (active, names: '|':_UIButtonBarButton:0x7fed8c463bb0 )>",
    "<NSLayoutConstraint:0x6000023e5680 UIButtonLabel:0x7fed8c457e90'Home'.centerY == _UIModernBarButton:0x7fed8c463e00'Home'.centerY + 1.5   (active)>",
    "<NSLayoutConstraint:0x6000023e4c80 '_UITemporaryLayoutHeight' _UIButtonBarButton:0x7fed8c463bb0.height == 0   (active)>",
    "<NSLayoutConstraint:0x6000023de490 'UIView-bottomMargin-guide-constraint' V:[UILayoutGuide:0x6000039eef40'UIViewLayoutMarginsGuide']-(16)-|   (active, names: '|':_UIButtonBarButton:0x7fed8c463bb0 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x6000023e5680 UIButtonLabel:0x7fed8c457e90'Home'.centerY == _UIModernBarButton:0x7fed8c463e00'Home'.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.
Bock answered 21/1, 2019 at 5:41 Comment(3)
See #47464320. Personally I consider this an iOS bug.Turki
I have had exactly the same constraint error when creating the toolbar using UIToolbar(), as pointed out above it's an iOS bug. The workaround as suggested by @strohtennis is to use init with CGRect param.Naldo
Not only do you need to set the height of the ToolBar, but it must be higher than each button's height to avoid triggering the warnings...Turkoman
S
60

using swift instead of: let toolbar = UIToolbar()

use: let toolBar = UIToolbar(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 35))

Schlegel answered 23/10, 2019 at 13:47 Comment(2)
The above fixed the issue. Seems like the height value when below 12 causing the constraint issue. I now create the UIToolbar using the init with CGRect as you suggested, set the translateAutoResizingmask to false and set the layout constraints.Naldo
THX for that comment! I isolated the behavior of warnings vs. height (if a button is taller than the frame's height, the warnings go off..) there's swift sample code in -> #61683044Turkoman
P
0

Using toolbar.sizeToFit() won't catch layoutConstraintsIssue unless you set UIToolbar's frame.

This issues happened to me before, and I solved by setting toolbar's frame.

Pyro answered 5/3, 2022 at 2:59 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.