美文网首页
Masonry使用

Masonry使用

作者: ios_stand | 来源:发表于2017-07-31 14:28 被阅读0次
  • (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *make))block;
  • (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker *make))block;
  • (NSArray *)mas_remakeConstraints:(void(^)(MASConstraintMaker *make))block;
// 定义这个常量,就可以不用在开发过程中使用"mas_"前缀。
#define MAS_SHORTHAND
// 定义这个常量,就可以让Masonry帮我们自动把基础数据类型的数据,自动装箱为对象类型。
#define MAS_SHORTHAND_GLOBALS
//  #import "Masonry.h"  这个头文件一定要放在上面两个宏的后面
mas_makeConstraints 这个方法只会添加新的约束
mas_updateConstraints 这个方法将会覆盖以前的某些特定的约束(更新约束)
mas_remakeConstraints 这个方法会将以前的所有约束删掉,添加新的约束
例:
//viewDidLoad
    [_view2 mas_makeConstraints:^(MASConstraintMaker *make) {
        __strong typeof(weakSelf) strongSelf = weakSelf;
        make.center.equalTo(strongSelf.view);
        make.size.mas_equalTo(CGSizeMake(300, 300));
    }];

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [_view2 mas_updateConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(150, 150));
    }];
}
//viewDidLoad
[_view2 mas_makeConstraints:^(MASConstraintMaker *make) {
        __strong typeof(weakSelf) strongSelf = weakSelf;
        make.center.equalTo(strongSelf.view);
        make.size.mas_equalTo(CGSizeMake(300, 300));
 }];
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [_view2 mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(150, 150));
    }];
}
//由于mas_remakeConstraints清除之前的所有约束,所以view2的x,y没有约束了,出现在(0,0)位置上
设置约束优先级
    UIView *redView = [[UIView alloc]init];
    redView.backgroundColor = [UIColor redColor];
    [self.view addSubview:redView];
    UIView *greenView = [[UIView alloc]init];
    greenView.backgroundColor = [UIColor greenColor];
    [self.view addSubview:greenView];
    UIView *blueView = [[UIView alloc]init];
    blueView.backgroundColor  = [UIColor blueColor];
    [self.view addSubview:blueView];
//在使用Masonry添加约束之前,需要在addSubview之后才能使用,否则会导致崩溃。

    [redView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(20);
        make.bottom.equalTo(self.view.mas_bottom).offset(-20);
        make.width.equalTo(self.view.mas_width).multipliedBy(0.2);
        make.height.equalTo(self.view.mas_height).multipliedBy(0.2);
    }];
    [greenView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(redView.mas_right).offset(20);
        make.bottom.equalTo(self.view.mas_bottom).offset(-20);
        make.width.equalTo(self.view.mas_width).multipliedBy(0.2);
        make.height.equalTo(self.view.mas_height).multipliedBy(0.2);
    }];
    [blueView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(greenView.mas_right).offset(20);
        make.bottom.equalTo(self.view.mas_bottom).offset(-20);
        make.width.equalTo(self.view.mas_width).multipliedBy(0.2);
        make.height.equalTo(self.view.mas_height).multipliedBy(0.2);
        make.left.equalTo(redView.mas_right).offset(20).priority(250);
      //make.left.equalTo(redView.mas_right).offset(20).priorityLow();
//Masonry为我们提供了三个默认的方法,priorityLow()、priorityMedium()、priorityHigh(),这三个方法内部对应着不同的默认优先级。
 //除了这三个方法,我们也可以自己设置优先级的值,可以通过priority()方法来设置。
//优先级的范围是0~1000,数字越大,优先级越高,在不设置的情况下默认为1000
//最后添加的这个约束的优先级是低的,这个约束只有在它的冲突约束被抹掉后,它才能实现
    }];


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.greenView removeFromSuperview];
    [UIView animateWithDuration:1.0f animations:^{
        [self.view layoutIfNeeded];
    }];
}
大于等于和小于等于某个值的约束
[self.textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    make.center.equalTo(self.view);
    // 设置宽度小于等于200
    make.width.lessThanOrEqualTo(@200);
    // 设置高度大于等于10
    make.height.greaterThanOrEqualTo(@(10));
}];
设置约束比例
// 设置当前约束值乘以多少,例如这个例子是redView的宽度是self.view宽度的0.2倍。
[self.redView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.center.equalTo(self.view);
    make.height.mas_equalTo(30);
    make.width.equalTo(self.view).multipliedBy(0.2);
}];

相关文章

网友评论

      本文标题:Masonry使用

      本文链接:https://www.haomeiwen.com/subject/vipllxtx.html