iOS Auto layout

Source: Internet
Author: User

Automatic layout:

1.Autoresizing

When UIView's autoresizessubviews is Yes, (by default yes), the child view in it automatically adapts its position and size to the Superview based on its own Autoresizingmask property. Note: Autoresizing can only set the relative relationship between the current control and the parent control, and screen fit

"Autoresizingmask is the left, right, top, bottom margin, and width and height of the child view proportional to the parent view, for example:

Uiviewautoresizingnone does not adjust automatically.

Uiviewautoresizingflexibleleftmargin automatically proportionally adjusts the distance to the left of the Superview, and the distance to the right of the superview is unchanged.

Uiviewautoresizingflexiblerightmargin automatically proportionally adjusts the right distance to the Superview, and the distance to the left of the Superview is unchanged.

The uiviewautoresizingflexibletopmargin automatically proportionally adjusts the top distance to the Superview, and the distance from the bottom of the superview is unchanged.

The uiviewautoresizingflexiblebottommargin automatically proportionally adjusts the bottom distance to the Superview, and the distance from the top of the Superview is unchanged.

Uiviewautoresizingflexiblewidth automatically adjusts the width proportionally.

The uiviewautoresizingflexibleheight automatically adjusts the height proportionally. 】

Related Demo:ui Basics day10-10 custom Layouts-code -02-implemented in code autoresizing

2.Autolayout (check use Auto Layout), generally need to set up or down or up or down four constraints, special controls do not necessarily have to 4 constraints

As the iphone releases Apple devices with different sizes of screens becoming more and more, not only does it require the ability to set relative positions based on the control's parent-child relationship, but it also requires the ability to set the position based on the relationship between any control

Because autoresizing can only set the relative relationship between the current control and the parent control, Autoresizing is powerless when it encounters the relationship between the sibling controls to be set. So you need AutoLayout to decide how to display this control by setting the relationship between a control and any other control, not just with parent-child controls

Added: "AutoLayout: Unable to meet set constraints, all screens are adaptable, so there is a sizeclass,size to differentiate the screen

Sizeclass is not wide: Divide the width height into three different types

Regualr

Compact

Any

When the width and height are of a certain type, a certain kind of screen is determined

So determine the class nine screen, just do not screen to distinguish between the specific constraints

Also requires AutoLayout implementation "

Note: autoresizing and AutoLayout are mutually exclusive, and only one can be used, and AutoLayout must be disabled when autoresizing is used. You can't use autoresizing when you use AutoLayout.

There are two core concepts in AutoLayout: References and constraints

Reference: Sets the position and size of the current control by referencing another control or parent control

Constraint constraint (rule): Restricting the position and size of a control by adding constraints

Steps to implement AutoLayout code

Create a specific constraint object using the Nslayoutconstraint class

Add a Constraint object to the corresponding view

-(void) AddConstraint: (Nslayoutconstraint *) constraint;

-(void) Addconstraints: (Nsarray *) constraints;

Code implementation AutoLayout Point of attention

1 "To prohibit the autoresizing function first, set the view below the property is no

View.translatesautoresizingmaskintoconstraints = NO;

2 before adding constraints, make sure that the controls are already on the respective parent control

3 "No more frame for view

Understand content, most of them use SB to set constraints

"A Nslayoutconstraint" object represents a constraint

Common ways to create constrained objects

+ (ID) Constraintwithitem: (id) view1 attribute: (nslayoutattribute) attr1 Relatedby: (nslayoutrelation) Relation Toitem :(id) view2 attribute: (nslayoutattribute) attr2 multiplier: (cgfloat) Multiplier constant: (cgfloat) C;

View1: The control to constrain

ATTR1: Type of constraint (how to constrain)

Relation: Relationship to the reference control

VIEW2: Referenced controls

ATTR2: Type of constraint (how to constrain)

Multiplier: Multiplier

C: constant

Example: [Nslayoutconstraint constraintwithitem:view1

Attribute:nslayoutattributetop

Relatedby:nslayoutrelationequal

Toitem:superview

Attribute:nslayoutattributetop

multiplier:1.0

Constant:padding.top]

Automatic layout has a core formula

Obj1.property1 = (Obj2.property2 * multiplier) + constant value

】

Frame: Masonry, "a lightweight layout framework with more" elegant "syntax to encapsulate automatic layouts" without the use of xib and storyboard, and with high readability

Mode one set Make.top.equalTo

Mode two continuous set Make.top.left.bottom.and.right.equalTo

Mode 31 Secondary Setting Make.edges.equalTo

Example: [View1 mas_makeconstraints:^ (Masconstraintmaker *make) {

Make.edges.equalTo (Superview). With.insets (padding);

}];

Snapkit:snapkit is the swift version of masonry, similar in approach to masonry

Example: view1.snp_makeconstraints {(make), Void in

Make.edges.equalTo (Superview). Insets (Uiedgeinsetsmake (10, 10, 10, 10))

This code means the margin of view1+uiedgeinsetsmake (10, 10, 10, 10) =superview

Steps to use the framework

1. First import the header file

2. In the place where the frame is used, import the main. h file

3. Use of the frame does not need to remove autoresizing

iOS Auto layout

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.