iOS Adaptive Layout Masonry (i)

Source: Internet
Author: User

Objective

Before the IPhone 5, we stayed on a 3.5-inch screen in the app layout, and how the Android cock Silk was so envious of iOS development that it wouldn't be bothered by a lot of screen adaptations. With the iphone product iteration, gradually appeared 4 inch, 4.7 inch, 5.5 inch ...., you say, iphone10+ how many inches? O (∩_∩) o~

Brief introduction

Because of the above-mentioned, the size of the screen gradually increased, but also the use of the previous method layout is obviously not possible, this time for the layout of the introduction of a new ending method Nsautolayout, but Apple provides more complex trouble, so someone on this basis for encapsulation, and provide an efficient framework- Masonary

Installation

How do I install the framework? or suggest using Cocoapods, here's what I wrote before cocoapods related usage

         pod ‘Masonry‘
Instructions for use

Using the Parameter Function description table

Open

1. Center display a view, and can always keep the width and height of the screen is less than 60 pixels

UIView *view1=[[UIView alloc]init];view1.backgroundColor=[UIColor brownColor];[self.view addSubview:view1];[view1 mas_makeConstraints:^(MASConstraintMaker *make) {    make.center.equalTo(self.view);    make.top.equalTo(self.view).with.offset(30);    make.left.equalTo(self.view).with.offset(30);    make.bottom.equalTo(self.view).with.offset(-30);    make.right.equalTo(self.view).with.offset(-30);}];

It is important to note that when the control is constrained with masonry, the control must be loaded into the parent view with Addsubview before it can be constrained, or it will cause a crash.

There are three functions in masonry

    • (Nsarray *) Mas_makeconstraints: (void (^) (Masconstraintmaker *make)) block;
    • (Nsarray *) Mas_updateconstraints: (void (^) (Masconstraintmaker *make)) block;
    • (Nsarray *) Mas_remakeconstraints: (void (^) (Masconstraintmaker *make)) block;

The mas_makeconstraints is only responsible for adding constraints, which can cause crashes when the function is called two times on the same control.

mas_updateconstraints update constraints, after defining constraints with mas_makeconstraints, you can supplement them with update constraints

The Mas_remakeconstraints function is more overbearing, removing all constraints that existed before the control, leaving only the most recent constraints inside the block function.
Above the

    make.center.equalTo(self.view);    make.top.equalTo(self.view).with.offset(30);    make.left.equalTo(self.view).with.offset(30);    make.bottom.equalTo(self.view).with.offset(-30);    

Do you want to be more elegant and better? Can do this:

UIEdgeInsets padding = UIEdgeInsetsMake(30, 30, -30, -30); make.center.equalTo(self.view); make.top.equalTo(self.view).with.offset(padding.top); make.left.equalTo(self.view).with.offset(padding.left); make.bottom.equalTo(self.view).with.offset(padding.bottom);  

We can also simplify the

You can also do this:

  

This article is a simple study of masonry, the next one will bring you advanced applications,_

iOS Adaptive Layout Masonry (i)

Related Article

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.