问题
最近用 UIToolBar
做 UI 需求, 只要一添加 UIBarButtonItem
上去就会有如下警告:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[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:0x600000f32ad0 _UIToolbarContentView:0x7fdd68555b90.width == 0>",
"<NSLayoutConstraint:0x600000f31ef0 _UIButtonBarStackView:0x7fdd68556d60.leading == _UIToolbarContentView:0x7fdd68555b90.leading + 20>",
"<NSLayoutConstraint:0x600000f314a0 _UIButtonBarStackView:0x7fdd68556d60.trailing == _UIToolbarContentView:0x7fdd68555b90.trailing - 20>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x600000f314a0 _UIButtonBarStackView:0x7fdd68556d60.trailing == _UIToolbarContentView:0x7fdd68555b90.trailing - 20>
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.
解决办法
问题原因是 UIToolBar
初始化时候直接使用了 init
方法.
1
UIToolbar *toolBar = [[UIToolbar alloc] init];
使用 initWithFrame:
初始化即可:
1
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(UIScreen.mainScreen.bounds), 44.0f)];
警告消失, 问题解决