Masonary的简单使用

Masonary是一个给控件添加约束条件的第三方库,它是轻量级的布局框架。它和autolayout作用差不多。

支持一下属性  

@property (nonatomic, strong, readonly) MASConstraint *left;
@property (nonatomic, strong, readonly) MASConstraint *top;
@property (nonatomic, strong, readonly) MASConstraint *right;
@property (nonatomic, strong, readonly) MASConstraint *bottom;
@property (nonatomic, strong, readonly) MASConstraint *leading;
@property (nonatomic, strong, readonly) MASConstraint *trailing;
@property (nonatomic, strong, readonly) MASConstraint *width;
@property (nonatomic, strong, readonly) MASConstraint *height;
@property (nonatomic, strong, readonly) MASConstraint *centerX;
@property (nonatomic, strong, readonly) MASConstraint *centerY;
@property (nonatomic, strong, readonly) MASConstraint *baseline;
 
使用1、将视图居中显示

UIView *view = [UIView new];

    view.backgroundColor = [UIColor redColor];

    [self.view addSubview:view];

    //添加新的约束条件

    [view mas_makeConstraints:^(MASConstraintMaker *make) {

        make.center.equalTo(self.view);

        make.size.mas_equalTo(CGSizeMake(200, 200));

    }];

2、在UIScrollView上排布些view自动计算尺寸

 UIScrollView *scrollView = [UIScrollView new];

    scrollView.backgroundColor = [UIColor blueColor];

    [view addSubview:scrollView];

    

    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.edges.equalTo(view).with.insets(UIEdgeInsetsMake(5, 5, 5, 5));

    }];

    

    UIView *container = [UIView new];

    

    container.backgroundColor = [UIColor blackColor];

    [scrollView addSubview:container];

    

    [container mas_makeConstraints:^(MASConstraintMaker *make) {

        make.edges.equalTo(scrollView);

        make.width.equalTo(scrollView);

    }];

    int count = 10;

    UIView *lastView = nil;

    

    for (int i = 1; i <= count; ++i) {

        UIView *subv = [UIView new];

        [container addSubview:subv];

        

        subv.backgroundColor = [UIColor colorWithHue:arc4random() % 256/256.0 saturation:arc4random() % 128/256.0 + 0.5 brightness:arc4random() % 128/256.0 + 0.5 alpha:1];

        [subv mas_makeConstraints:^(MASConstraintMaker *make) {

            make.left.and.right.equalTo(container);

            make.height.mas_equalTo(@(20*i));

           

            if (lastView) {

                make.top.mas_equalTo(lastView.mas_bottom);

            }

            else{

                make.top.mas_equalTo(container.mas_top);

            }

        

        }];

        lastView = subv;

             }

    [container mas_makeConstraints:^(MASConstraintMaker *make) {

        make.bottom.equalTo(lastView.mas_bottom);

    }];

posted @ 2014-12-29 12:01  我知道是菜鸟  阅读(304)  评论(0编辑  收藏  举报