Use third-party framework masonry for simple use of automatic layouts

Source: Internet
Author: User

Apple's native automatic layout scheme is too cumbersome to introduce a simple and useful third-party framework.

1//Set constraint 2-(Nsarray *) Mas_makeconstraints: (void (^) (Masconstraintmaker *)) Block;3 4//If a constraint has been previously, update the new constraint, or add a constraint if there is no constraint 5 -(Nsarray *) Mas_updateconstraints: (void (^) (Masconstraintmaker *)) BLOCK;6 7//Remove all previous constraints, add new constraint 8-(Nsarray *) Mas_ Remakeconstraints: (void (^) (Masconstraintmaker *make)) block;
Note:
The Translatesautoresizingmaskintoconstraints property of the custom control should be set to no before use;

Add two controls

UIView *blueview = [[UIView alloc] init];

Blueview.backgroundcolor = [Uicolor Bluecolor];

Blueview.translatesautoresizingmaskintoconstraints = NO;

[Self.view Addsubview:blueview];

UIView *redview = [[UIView alloc] init];

Redview.backgroundcolor = [Uicolor Redcolor];

Redview.translatesautoresizingmaskintoconstraints = NO;

[Self.view Addsubview:redview];

Set constraints for Blue view

[Blueview mas_makeconstraints:^ (Masconstraintmaker *make) {

Make.left.equalTo (self.view.mas_left). Offset (30);//And the left margin of the parent view is 30;

Make.bottom.equalTo (Self.view.mas_bottom). Offset (-30);//And the bottom spacing of the parent view is 30;

Make.right.equalTo (redview.mas_left). Offset (-30);//And a red view with a spacing of 30;

Make.height.mas_equalTo (50);//Blue view has a height of 50

}];

To set a constraint on a red view

[Redview mas_makeconstraints:^ (Masconstraintmaker *make) {

Make.right.equalTo (self.view.mas_right). Offset (-30);//And the right-hand spacing of the parent view is 30;

Make.bottom.equalTo (Blueview.mas_bottom);//align with the bottom of the blue view

Make.height.equalTo (blueview.mas_height);//The height of the blue view is equal to

Make.width.equalTo (blueview.mas_width);//And the width of the blue view is equal

}];

Use third-party framework masonry for simple use of automatic layouts

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.