Masonry for iOS Adaptive Layout (I)
Preface
Before the iPhone 5, we stayed at the 3.5 screen stage for the app layout. At that time, Android silk was so envious of iOS development that it would not be troubled by a lot of screen adaptation. With the iteration of iPhone products, the four-inch, 4.7-inch, 5.5-inch ...., You said, how many inch will iPhone 10 + be there? O (distinct _ distinct) O ~
Introduction
As mentioned above, the screen size is gradually increased, and it is obviously not feasible to use the previous layout method. At this time, a new ending method NSAutoLayout is introduced for the layout, however, Apple is complicated and troublesome, so some people encapsulate it and provide an efficient framework-Masonary
Install
How to install the framework? We recommend that you use CocoaPods. Here I have previously written CocoaPods usage.
pod 'Masonry'
Instructions for use
Parameters
Kaibei
1. center a View and keep 60 pixels less than the width and height of the screen at all times
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);}];
Note that when the control is constrained by Masonry, you must first load the control with addSubview to the parent view to apply the constraint. Otherwise, the control will 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;
Mas_makeConstraints is only responsible for adding constraints. When you call this function twice for the same control, it will cause a crash. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Examples/NDC1LzK + examples/ydLU08O4/NDC1LzK + examples/examples + KGjPGJyIC8 + dq1_z8pmtcq8l3a + DQo8cHJlIGNsYXNzPQ = "brush: java;"> 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);
Do you want to be more elegant and better? You 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); make.right.equalTo(self.view).with.offset(padding.right);
We can also simplify it
make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(10, 10, 10, 10));
You can also do this:
make.top.left.bottom.and.right.equalTo(self.view).with.insets(UIEdgeInsetsMake(10, 10, 10, 10));