刚开始使用 Autolayout 遇到下面的警告人容易让人气馁,常常不知所措而放弃了使用 Autolayout。c++
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)
(...........)
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
复制代码
正如输出中所述,Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger
,如今介绍下使用 UIViewAlertForUnsatisfiableConstraints
的调试方法。bash
在 UIViewAlertForUnsatisfiableConstraints
添加 symbolic breakpoint
:ide
再次调试的时候就能够经过 lldb 来调试了,然并卵,若是你不知道 lldb 的话。oop
因此交给你一个小技巧,添加布局
po [[UIWindow keyWindow] _autolayoutTrace] // OC项目
expr -l objc++ -O -- [[UIWindow keyWindow] _autolayoutTrace] // Swift项目
复制代码
这样就能够直接看到输出:动画
(lldb) po [[UIWindow keyWindow] _autolayoutTrace]
UIWindow:0x7f9481c93360
| •UIView:0x7f9481c9d680
| | *UIView:0x7f9481c9d990- AMBIGUOUS LAYOUT for UIView:0x7f9481c9d990.minX{id: 13}, UIView:0x7f9481c9d990.minY{id: 16}
| | *_UILayoutGuide:0x7f9481c9e160- AMBIGUOUS LAYOUT for _UILayoutGuide:0x7f9481c9e160.minY{id: 17}
| | *_UILayoutGuide:0x7f9481c9ebb0- AMBIGUOUS LAYOUT for _UILayoutGuide:0x7f9481c9ebb0.minY{id: 27}
复制代码
其中 AMBIGUOUS 相关的视图就是约束有问题的。0x7f9481c9d990就是有问题视图的首地址。ui
固然进一步的调试须要 lldb 的命令。好比,打印视图对象this
(lldb) po 0x7f9481c9d990
<UIView: 0x7f9481c9d990; frame = (0 0; 768 359); autoresize = RM+BM; layer = <CALayer: 0x7fc82d338960>>
复制代码
改变颜色:spa
(lldb) expr ((UIView *)0x174197010).backgroundColor = [UIColor redColor]
(UICachedDeviceRGBColor *) $4 = 0x0000000174469cc0
复制代码
剩下的就是去代码中找到这个视图,而后修改其约束了。debug
UIView 是咱们常用的一个基本控件,其中有几个基本的布局方法须要清楚。
layoutSubViews:
添加子视图常重写这个方法,这个方法是用来从新布局子视图的,经常使用于对子视图布局,或者在其余方法中调用以达到从新布局的做用。
setNeedsLayout
告知页面须要更新,可是不会马上开始更新,执行后会马上调用layoutSubviews
。
layoutIfNeeded
告知页面布局马上更新,因此通常都会和setNeedsLayout
一块儿使用。若是但愿马上生成新的frame
须要调用此方法,利用这点通常布局动画能够在更新布局后直接使用这个方法让动画生效。
setNeedsUpdateConstraints
告知须要更新约束,可是不会马上开始
updateConstraintsIfNeeded
告知马上更新约束
updateConstraints
系统更新约束
layoutSubviews
的时机init
方法初始化不会触发layoutSubviews
,可是是用initWithFrame
进行初始化时,当rect
的值不为CGRectZero
时,会触发。addSubview
方法会触发layoutSubviews
。view
的Frame
会触发layoutSubviews
,前提是frame
的值设置先后发生了变化。UIScrollView
会触发layoutSubviews
。Screen
会触发父UIView
上的layoutSubviews
。UIView
大小的时候也会触发父UIView
上的layoutSubviews
。注意:layoutSubViews 在 drawRect 以前调用。
在使用 AutoLayout 的时候可能也会同时也会用到 frame,好比须要用到 layer 的时候,想让 layer 的尺寸是由其它视图尺寸设定的,而这个视图又是由约束控制布局的,若是将 layer 的初始化与 view 的初始化放在一个方法中; 好比:
layer.bounds = CGRectMake(0,0,view.bounds.size.widith * 0.5,50)
复制代码
那么极可能拿到 layer 的宽度是0。
好比:
UIView *redView = [[UIView alloc] init];
redView.backgroundColor = [UIColor redColor];
[self.view addSubview:redView];
self.redView = redView;
// 设置约束
[redView mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self.view.mas_centerX);
make.centerY.equalTo(self.view.mas_centerY);
make.size.mas_equalTo(CGSizeMake(150, 80));
}];
NSLog(@"self.view 的尺寸%@,redView 的尺寸%@",self.view,redView);
2017-06-08 15:32:51.815107+0800 MasonryDemo[42940:1076244] self.view 的尺寸<UIView: 0x7fd8cd408960; frame = (0 0; 414 736); autoresize = W+H; layer = <CALayer: 0x604000227200>>,redView 的尺寸<UIView: 0x7fd8cd407650; frame = (0 0; 0 0); layer = <CALayer: 0x6040002274a0>>
复制代码
这个时候,看到为何设置了约束,而打印出来的 frame 是 (0 0; 0 0),是由于约束被设置以后它并不会当即对 view 做出改变,而是要等到 layout 时,才会对视图的尺寸进行修改,而 layout 一般是在视图已经加载到父视图上面时作出响应。
因此若是在 viewDidLoad 中设置了约束,那么要等到 viewDidAppear 时 view 的尺寸才会真正改变。
解决办法:
- (void)testLayout {
UIView *redView = [[UIView alloc] init];
redView.backgroundColor = [UIColor redColor];
[self.view addSubview:redView];
self.redView = redView;
// 设置约束
[redView mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self.view.mas_centerX);
make.centerY.equalTo(self.view.mas_centerY);
make.size.mas_equalTo(CGSizeMake(150, 80));
}];
}
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
NSLog(@"self.view 的尺寸%@,redView 的尺寸%@",self.view,self.redView);
}
2017-06-08 15:50:41.621147+0800 MasonryDemo[43363:1089098] self.view 的尺寸<UIView: 0x7fe412f0f780; frame = (0 0; 414 736); autoresize = W+H; layer = <CALayer: 0x604000238b00>>,redView 的尺寸<UIView: 0x7fe412e045b0; frame = (132 328; 150 80); layer = <CALayer: 0x60000003c460>>
复制代码
一、把获取 frame 的设置写到 layoutSubviews 中或者写到 viewDidLayoutSubviews 中便可。由于 layout 约束生效时 view 的 center 或者 bounds 就会被修改,当 center 或者 bounds 被修改时layoutSubview 就会被调用,随后 viewDidLayoutSubviews 就回被调用。这个时候,设置约束的视图 frame 就再也不是 (0,0,0,0) 了。
- (void)testLayout {
UIView *redView = [[UIView alloc] init];
redView.backgroundColor = [UIColor redColor];
[self.view addSubview:redView];
self.redView = redView;
// 设置约束
[redView mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self.view.mas_centerX);
make.centerY.equalTo(self.view.mas_centerY);
make.size.mas_equalTo(CGSizeMake(150, 80));
}];
[redView setNeedsLayout];
[redView layoutIfNeeded];
NSLog(@"self.view 的尺寸%@,redView 的尺寸%@",self.view,redView);
}
2017-06-08 15:52:32.749105+0800 MasonryDemo[43419:1090641] self.view 的尺寸<UIView: 0x7fe36440b5f0; frame = (0 0; 414 736); autoresize = W+H; layer = <CALayer: 0x604000422100>>,redView 的尺寸<UIView: 0x7fe364405040; frame = (-75 -40; 150 80); layer = <CALayer: 0x6040004207a0>>
复制代码
二、若是将约束和 frame 写在同一方法中,写完约束就设置 frame,而不是想把 frame 的设置放到 layoutSubview 中,好比设置好约束后立刻就想根据约束的结果计算高度,那么必须在设置完约束以后手动调用 setNeedsLayout 和 layoutIfNeeded 方法,让视图当即 layout,更新 frame,可是这个时候就能够拿到真实的 size 并不能拿到真实的 center ,不建议这么使用。
- (void)testLayout {
UIView *redView = [[UIView alloc] init];
redView.backgroundColor = [UIColor redColor];
[self.view addSubview:redView];
self.redView = redView;
// 设置约束
[redView mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self.view.mas_centerX);
make.centerY.equalTo(self.view.mas_centerY);
make.size.mas_equalTo(CGSizeMake(150, 80));
}];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"self.view 的尺寸%@,redView 的尺寸%@",self.view,redView);
});
}
2017-06-08 15:55:56.282546+0800 MasonryDemo[43500:1092911] self.view 的尺寸<UIView: 0x7fda85e0d540; frame = (0 0; 414 736); autoresize = W+H; layer = <CALayer: 0x600000233620>>,redView 的尺寸<UIView: 0x7fda85e0c770; frame = (132 328; 150 80); layer = <CALayer: 0x600000233540>>
复制代码
三、在 dispatch_after 里面能够拿到真实的 frame ,或许是由于设置约束和获取 frame 不在同一个 runloop 的缘由吧。