Masonry第三方约束 (2016.4.14更新)

cocospads


add file


  • Masonry_github 只使用Masonry文件夹内容
  • 使用cocospad 安装 product -> cocoaPods ->install pods (注意路径)
  • gem_path: /usr/local/bin
  • install 后一等一会 会有输出提示

image

代码设置


 // 只要添加了这个宏,就不用带mas_前缀
  #define MAS_SHORTHAND
 // 只要添加了这个宏,equalTo就等价于mas_equalTo
 #define MAS_SHORTHAND_GLOBALS
 // 这个头文件一定要放在上面两个宏的后面
 #import "Masonry.h"

约束的类型


尺寸:width\height\size
边界:left\leading\right\trailing\top\bottom
中心点:center\centerX\centerY
边界:edges

约束优先级


//“width最少也大于200”,并且给这个约束设置优先级比 “右边要比父控件少50” 的优先级高
	[contentLbl2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.top.equalTo(blackView).with.offset(10);
        make.width.greaterThanOrEqualTo(@200).priority(900);
        make.right.equalTo(blackView).with.offset(-50).priority(800);
        make.height.equalTo(@30);
    }];

无优先级

image

有优先级 (数值越大优先级越高)

image

添加约束动画


[UIView animateWithDuration:0.2 animations:^{
	//计算其他控件的fram
	[self setModeSelec];
	[self flashLab];
	[self flashView];
	[self flashSelectCur];
	//立即刷新约束限制
	[self.view layoutIfNeeded];
}];

约束更新


 // 告诉self.view约束需要更新
  [self.view setNeedsUpdateConstraints];
  // 调用此方法告诉self.view检测是否需要更新约束,若需要则更新,下面添加动画效果才起作用
  [self.view updateConstraintsIfNeeded];

  [UIView animateWithDuration:0.3 animations:^{
    [self.view layoutIfNeeded];
  }];

动态改变UIScrollView.contentSize大小


//注意先后顺序
- (void)setup{
    [self setClass];
    [self setScroll];
}
- (void)setScroll{
    self.scrollView.frame = CGRectMake(0, 0, PKScreenW, PKScreenH-PKNavigationH);
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
        make.bottom.mas_equalTo(self.topicBackGround.mas_bottom).with.offset(20);
    }];
}

- (void)setClass{
	//对要布局的文件添加子view,计算一堆坐标
    [self.topicTitle setText:@"选择话题(必选)"];
    NSArray * ary = [NSArray arrayWithObjects:@"评车",@"驾车",@"用车",@"品车",nil];
    CGFloat orgY = 0;
    for (int i = 0; i < 4; i++) {
        PKLabelSelectionTopic *v = [[PKLabelSelectionTopic alloc]initWithString:ary[i]];
        v.delegate = self;
        v.frame = CGRectMake(0, orgY, PKScreenW, 48);
        v.tag = i;
        orgY += 48;
        [self.topicBackGround addSubview:v];
        [self.classObjAry addObject:v];
    }
    //对要需要约束的元件添加约束
    [self.topicBackGround mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.labBackGround.mas_bottom).with.offset(48);
        make.left.equalTo(self.labBackGround.mas_left);
        make.width.equalTo(self.labBackGround.mas_width);
        make.height.equalTo(@(48*4));
    }];    
}

ps:红色框下边沿为动态改变UIScrollView.contentSize的下边沿边界

image

居中显示


 1   // 居中显示
 2   UIView *redView = [[UIView alloc] init];
 3   redView.backgroundColor = [UIColor redColor];
 4   [self.view addSubview:redView];
 5 
 6   // 可以使用三个方法来添加约束。
 7   [redView mas_makeConstraints:^(MASConstraintMaker *make) {
 8       make.centerX.equalTo(self.view);
 9       make.centerY.equalTo(self.view);
10       make.height.equalTo(100);
11       make.width.equalTo(200);
12   }];

并排位于底部,间距20


image

   //并排位于底部,间距20  
    UIView *redView = [[UIView alloc] init];
    redView.backgroundColor = [UIColor redColor];
    [self.view addSubview:redView];
  
    UIView *blueView= [[UIView alloc] init];
    blueView.backgroundColor = [UIColor blueColor];
    [self.view addSubview:blueView];
 
   // 添加约束
   [redView makeConstraints:^(MASConstraintMaker *make) {
       make.left.equalTo(self.view.left).offset(20); // 左边间距20
       make.right.equalTo(blueView.left).offset(-20); // 右边和blueView间距20
make.bottom.equalTo(self.view.bottom).offset(-20); // 底部间距20
 
       make.height.equalTo(200); // 高度200
   }];
 
   [blueView makeConstraints:^(MASConstraintMaker *make) {
       make.right.equalTo(self.view.right).offset(-20); // 右边间距20
       make.bottom.equalTo(redView.bottom); // 和redView底部间距相同
 
       make.height.equalTo(redView); // 等宽
       make.width.equalTo(redView); // 等高
 }];