Autolayout is an excellent third-party open-source library and autolayout is an open-source library.
Today, we found that CSDN supports markdown... Or to the new blog address: an excellent third-party open-source library of Autolayout
Recently, the project began to use code-only layout of the entire UI framework. For me who have been using xib + storyboard controls for a long time ago, the amount of code written every day has increased dramatically... ForGood sb or a good argument about pure code
For more information, see the quick analysis.
When it comes to UI adaptation layout, there are two methods:
* Frame, using[UIScreen mainScreen].bounds.size.width/height
One by one, the corresponding frame is calculated, and the result is certainly OK, but the process is very painful, especially for students in mathematics taught by a Chinese teacher like me.
* Autolayout: I believe it must be the mainstream layout. It is also very convenient for the UI Designer to directly set the constraint value without any other computation.
This section mainly introduces the autolayout constraint, because the system provides the api to writeSmelly and long
, We recommend a good third party.
The OC version correspondsMasonry
The Swift version correspondsSnapKit
Of course, there are other excellent third parties related to autolayout, which will be provided at the end. Interested friends can study it. LZ mainly usesMasonry
(Love this chain syntax) ^_^!
Masonry
Github address: Masonry
The specific usage has been clearly stated on github. You can download the Demo to see the implementation of the chestnut and summarize the usage.
*mas_makeConstraints
Use the most, make initial constraints, only add Constraints
*mas_updateConstraints
Formake
The constraint is updated. Generally, no new constraint is added, but the original constraint value is modified.
*mas_remakeConstraints
Clear all previous constraints, use the latest constraints set in it, often used for constraint setting of new positions after Animation
* High Priority.priorityHigh
Low priority.priorityLow
Which constraint is usually used to determine the priority response according to the condition?
*multipliedBy(x)
= * X,dividedBy(x)
=/X
* "'
Make. edges. mas_pointer to (UIEdgeInsetsMake (10, 10, 10 ));
// Edges is equivalent to top-left-bottom-right.
// The offset values in top and left are positive.
// Why is the offset in the bottom and right negative? Because the absolute bottom calculated here must be smaller than the bottom height of sv, so we need to use the same-10 for the right
// What's interesting is that the and with functions do not actually do anything. They can be omitted.
/* Is equivalent to make. top. similar to (weakSelf. sv ). with. offset (10); make. left. similar to (weakSelf. sv ). with. offset (10); make. bottom. similar to (weakSelf. sv ). with. offset (-10); make. right. similar to (weakSelf. sv ). with. offset (-10 );*/
''
UIScrollView
It is a special view. The best way to put other views in it is to put one first.containerView
Setedges
Equal, and then add subview on this view.
Simulate the bottom View of the UITabBarController to set the number and interval:
- (void)simulateSystemTabBarWithButtonCount:(NSInteger)count withSpace:(CGFloat)space {[self.view showPlaceHolder];self.view.backgroundColor = [UIColor grayColor];NSMutableArray *viewArray = [NSMutableArray arrayWithCapacity:10];for (int i = 0; i < count; i++) { UIView *view = [UIView new]; view.backgroundColor = [UIColor colorWithHue:( arc4random() % 256 / 256.0 ) saturation:( arc4random() % 128 / 256.0 ) + 0.5 brightness:( arc4random() % 128 / 256.0 ) + 0.5 alpha:1];; [view showPlaceHolder]; [self.view addSubview:view]; [viewArray addObject:view];}UIView *lastView = viewArray.lastObject;for (int i = 0; i < count; i++) { UIView *view = (UIView *)viewArray[i]; [view mas_makeConstraints:^(MASConstraintMaker *make) { make.bottom.equalTo(view.superview); make.height.equalTo(@49); make.width.equalTo(lastView); if (i == 0) { make.left.mas_equalTo(view.superview).offset(space); } else { UIView *frontView = (UIView *)viewArray[i-1]; make.left.equalTo(frontView.mas_right).offset(space); (i == viewArray.count - 1) ? make.right.mas_equalTo(view.superview).offset(-space) : nil; } }];}}
Take count = 4 and space = 0 as Examples
- When an animation is implemented, it is called after the constraint is updated
layoutIfNeeded
- Finally, I want to emphasize that it is really easy to use !!!
SnapKit
Github address: SnapKit
PS: currently only supports swift1.2 version, in the latest xcode7-swift2.0 will report an error, I asked the author on Twitter, he said recently busy, temporarily unavailable... Else... If you are interested, you can change it to 2.0 and then pull... Me? Still under study.
This is just a few examples.Masonry
The same, and I used the Demo below.Snapkit
To write (xcode7 is a Drag Control... No way. Who does not support 2.0)
For example, the most common buttons are:
Other materials
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.