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)