Masonary的簡單使用

來源:互聯網
上載者:User

標籤:

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);

    }];

Masonary的簡單使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.