深入理解IOS控件布局之Masonry布局框架

2020-01-18 20:49:41于丽

等价于


  UIView *tempView=[[UIView alloc]init];
  tempView.backgroundColor=[UIColor greenColor];
  [self.view addSubview:tempView];
  
  [tempView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.view.mas_left).offset(50);
    make.right.equalTo(self.view.mas_right).offset(-50);
    make.top.equalTo(self.view.mas_top).offset(50);
    make.bottom.equalTo(self.view.mas_bottom).offset(-50);
  }];

也可以简化为下面这种


  UIView *tempView=[[UIView alloc]init];
  tempView.backgroundColor=[UIColor greenColor];
  [self.view addSubview:tempView];
  
  [tempView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.edges.mas_equalTo(UIEdgeInsetsMake(50, 50, 50, 50));
  }];

又等价于


  UIView *tempView=[[UIView alloc]init];
  tempView.backgroundColor=[UIColor greenColor];
  [self.view addSubview:tempView];
  
  [tempView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(50, 50, 50, 50));
  }];

更新约束


  [tempView mas_updateConstraints:^(MASConstraintMaker *make) {
    make.left.mas_equalTo(50);
    make.right.mas_equalTo(-50);
    make.top.mas_equalTo(100);
    make.bottom.mas_equalTo(-100);
  }];

清除之前的约束保留最新的


  [tempView mas_remakeConstraints:^(MASConstraintMaker *make) {
    make.left.mas_equalTo(100);
    make.right.mas_equalTo(-100);
    make.top.mas_equalTo(100);
    make.bottom.mas_equalTo(-100);
  }];

特别注意:

声明约束必须在视图添加到父试图上面之后调用。

4.)mas_equalTo与equalTo

上面的举例中分别使用了mas_equalTo和equalTo达到了同样的效果,我在刚开始使用Masonry的时候很容易混淆他们两个,今天特意分析一下他们的区别。mas_equalTo是一个MACRO,比较的是值,equalTo比较的是id类型。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持ASPKU。


注:相关教程知识阅读请移步到IOS开发频道。