谈谈 Autolayout

关于Autolayout的调试

刚开始使用 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 breakpointide

  • 打开断点导航(cmd+7)
  • 点击左下角的+按钮
  • 选择Add Symbolic Breakpoint
  • 在Symbol添加UIViewAlertForUnsatisfiableConstraints

image.png

再次调试的时候就能够经过 lldb 来调试了,然并卵,若是你不知道 lldb 的话。oop

因此交给你一个小技巧,添加布局

po [[UIWindow keyWindow] _autolayoutTrace] // OC项目
expr -l objc++ -O -- [[UIWindow keyWindow] _autolayoutTrace] // Swift项目
复制代码

image.png

这样就能够直接看到输出:动画

(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

AutoLayout 关于 update 的几个方法

UIView 是咱们常用的一个基本控件,其中有几个基本的布局方法须要清楚。

  • layoutSubViews: 添加子视图常重写这个方法,这个方法是用来从新布局子视图的,经常使用于对子视图布局,或者在其余方法中调用以达到从新布局的做用。

  • setNeedsLayout 告知页面须要更新,可是不会马上开始更新,执行后会马上调用layoutSubviews

  • layoutIfNeeded 告知页面布局马上更新,因此通常都会和setNeedsLayout一块儿使用。若是但愿马上生成新的frame须要调用此方法,利用这点通常布局动画能够在更新布局后直接使用这个方法让动画生效。

  • setNeedsUpdateConstraints 告知须要更新约束,可是不会马上开始

  • updateConstraintsIfNeeded 告知马上更新约束

  • updateConstraints 系统更新约束

触发layoutSubviews的时机

  • init方法初始化不会触发layoutSubviews,可是是用initWithFrame 进行初始化时,当rect的值不为CGRectZero时,会触发。
  • addSubview方法会触发layoutSubviews
  • 设置viewFrame会触发layoutSubviews,前提是frame的值设置先后发生了变化。
  • 滚动一个UIScrollView会触发layoutSubviews
  • 旋转Screen会触发父UIView上的layoutSubviews
  • 改变一个UIView大小的时候也会触发父UIView上的layoutSubviews

注意:layoutSubViews 在 drawRect 以前调用。

AutoLayout 与 Frame

在使用 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 的缘由吧。

相关文章
相关标签/搜索