iOS Adaptive Library---Masonry use

Source: Internet
Author: User

Masonry is a lightweight layout framework with its own description syntax and a more elegant chained syntax for automatic layout, concise and highly readable, and supports both IOS and Max OS x. Masonry is a library that uses code to write iOS or OS interfaces instead of auto layout. Masonry's github address:https://github.com/SnapKit/Masonry

The following will be introduced from three aspects as follows:

-Masonry Configuration -Masonry Use -Masonry Instances

1 Masonry configuration

-We recommend using the Pods method to introduce the class library, pod ' masonry ', if you do not know how to use the pod, you can refer to the article: improve iOS development efficiency tools

Then-introduce the header file #import "Masonry.h".

Masonry use explanation

Mas_makeconstraints is to add constraints to the view, there are several constraints, respectively, is the margin, width, height, left upper right lower distance, baseline. After the constraint has been added, there can be corrections, fixed offset (displacement) correction and multipliedby (magnification) correction.

The syntax is generally make.equalto or make.greaterthanorequalto or Make.lessthanorequalto + multiple and displacement corrections.

Note Point 1: elements that use the Mas_makeconstraints method must be added to the parent element beforehand, such as [Self.view Addsubview:view];

Note the difference between 2:mas_equalto and Equalto: Mas_equalto is more than Equalto type conversion operations, generally speaking, most of the time two methods are common, but for numeric elements use Mas_equalto. For the handling of objects or multiple properties, use Equalto. In particular, multiple attributes must be used Equalto, such as Make.left.and.right.equalTo (Self.view);

Note Point 3: Notice the method with and and, this even a method actually does not do any operation, the method simply returns the object itself, this method of the left and right is purely for the method to write the time of readability. Make.left.and.right.equalTo (Self.view), and Make.left.right.equalTo (Self.view), are exactly the same, but the obvious addition of the and method of the statement is more readable.

Masonry primary Use examples

EXP1: The center point is the same as the Self.view, with a width of 100*400 300*200

-(void) exp1{

UIView *view = [UIView new];

[View setbackgroundcolor: [uicolor redcolor]];

[self. View addsubview: view];

[View mas_makeconstraints: ^ (masconstraintmaker *make) {

Make. Center. Equalto(self. View);

Make. size. Mas_equalto(Cgsizemake (+));

}];

UIView *view1=[UIView new];

[View1 setbackgroundcolor: [uicolor blackcolor]];

[self. View addsubview: view1];

[View1 mas_makeconstraints: ^ (masconstraintmaker *make) {

Make. Center. Equalto(self. View);

Make. size. Mas_equalto(Cgsizemake (+ ));

}];

}

EXP2: Both the upper and lower left and right margins are 10

-(void) exp2{

UIView *view = [UIView new];

[View Setbackgroundcolor:[uicolor Redcolor];

[Self.view Addsubview:view];

[View mas_makeconstraints:^ (Masconstraintmaker *make) {

Make.edges.equalTo (Self.view). With.insets (Uiedgeinsetsmake (10, 10, 10, 10));

Make.top.equalTo (Self.view). With.offset (10);

Make.left.equalTo (Self.view). With.offset (10);

Make.bottom.equalTo (Self.view). With.offset (-10);

Make.right.equalTo (Self.view). With.offset (-10);

}];

}

iOS Adaptive Library---Masonry use

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.