Autolayout--purelayout

Source: Internet
Author: User

Two previous articles: iOS: Using AutoLayout in Code (1) – Scaling and prioritizing and IOS: Using AutoLayout (2) –intrinsiccontentsize and content hugging in your code Priority tells about using code in iOS to write AutoLayout, and readers can see that writing AutoLayout in code is tedious and error-prone. There are also a number of alternatives, such as Apple's official visual Format Language, and some heavyweight projects like Masonry, which introduce a lightweight, iOS-and OS X-enabled engineering Purelayout, The reason for the lightness is because Purelayout no longer joins a set of its own grammar, but in the form of a category to assist Apple NSLayoutConstraint 's existing set of things, small size, writing a lower level and there is no lack of readability.

For example, the above simple two yellow box program, write faster with Purelayout.

First, the Purelayout source code is added to the project, or installed with Cocoapods ( podilfe add pod ‘PureLayout‘ ).

Then add an auxiliary function that creates the view:

-(uiview*) CreateView {
There are autolayout do not need to set frame
UIView *view = [UIView new];
View.backgroundcolor = [Uicolor Yellowcolor];
Do not allow autoresizingmask conversion to AutoLayout, Purelayout will help you set the internal.
View.translatesautoresizingmaskintoconstraints = NO;

return view;
}

viewDidLoadCreate two view in, and then add Purelayout to the AutoLayout in the Constaint is OK, the code is very good understanding:

Create a two view
UIView *view1 = [self createview];
UIView *view2 = [self createview];

Addsubview
[Self.view Addsubview:view1];
[Self.view Addsubview:view2];

Set View1 height to 70
[View1 autosetdimension:aldimensionheight tosize:70.0];

Both View1 and View2 are 20 from the parent view margin
Aledgeinsets definsets = Aledgeinsetsmake (20.0, 20.0, 20.0, 20.0);
[View1 autopinedgestosuperviewedgeswithinsets:definsets Excludingedge:aledgebottom];
[View2 autopinedgestosuperviewedgeswithinsets:definsets excludingedge:aledgetop];

The distance between two view is also 20
[View2 autopinedge:aledgetop toedge:aledgebottom ofview:view1 WithOffset:defInsets.bottom];

The result is the same as above:

Original address: http://www.mgenware.com/blog/?p=2335



Wen/ray_win (author of Jane's book)
Original link: http://www.jianshu.com/p/2bebe9ef24fd
Copyright belongs to the author, please contact the author to obtain authorization, and Mark "book author".

Autolayout--purelayout

Related Keywords:

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.