Autolayout is an excellent third-party open-source library and autolayout is an open-source library.

Source: Internet
Author: User

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 codeFor more information, see the quick analysis.
When it comes to UI adaptation layout, there are two methods:
* Frame, using[UIScreen mainScreen].bounds.size.width/heightOne 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 correspondsMasonryThe Swift version correspondsSnapKitOf 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_makeConstraintsUse the most, make initial constraints, only add Constraints
*mas_updateConstraintsFormakeThe constraint is updated. Generally, no new constraint is added, but the original constraint value is modified.
*mas_remakeConstraintsClear all previous constraints, use the latest constraints set in it, often used for constraint setting of new positions after Animation
* High Priority.priorityHighLow priority.priorityLowWhich 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 );*/

''

  • UIScrollViewIt is a special view. The best way to put other views in it is to put one first.containerViewSetedgesEqual, 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 updatedlayoutIfNeeded
  • 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.MasonryThe same, and I used the Demo below.SnapkitTo write (xcode7 is a Drag Control... No way. Who does not support 2.0)
For example, the most common buttons are:

Other materials
  • Other third-party open-source libraries of autolayout

    PureLayout
    UIView-AutoLayout
    MMPlaceHolder: A helper tool with a display width * high
    Cartography for swift

  • Blog

    Advanced Automatic Layout toolbox
    AutoLayout: Forget Frame and embrace Constraint
    Autolayout Basics
    Auto Layout Guide official
    Introduction and use of Masonry (Quick Start With Autolayout)
    Interesting Autolayout example-Masonry implementation
    Introduction to meoshen's Autolayout

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.