Objective-c--ui Foundation Development Day (Automatic layout)

Source: Internet
Author: User

First, the use of autoresizing (understand)

Only the parent control can be referenced

1, to achieve the screen switching, can not be the control of the frame capillary, the need for screen adaptation

2. You need to refer to the parent control

Use Auto layout disabled

will appear autoresizing

3, AutoLayout and autoresizing are mutually exclusive.

Do not run to see situations in different sizes

preview-"Mainstoryboard

4, the outer side of the four line check, indicating that the view distance from the border around the parent control is unchanged

The two lines on the inside indicate that the width and height will vary with the parent view (proportional changes

Second, the implementation of autoresizing code and its drawbacks

6 lines, outside four controls the distance from the parent control

The relative changes between the inside two records and the parent control

Autoresizingmask

Uiviewautoresizingflexibletopmargin

Uiviewautoresizingnone

Uiviewautoresingflexibleheight

Go to | or No.

Autoresing only for sub-view and parent view

Sub View and sub View directly cannot be set

Just remember to use the 6 wires

AutoLayout (using disease mastery)

Third, the basic use of AutoLayout

AutoLayout Core ideas: References and constraints

You can set constraints on controls and any controls

Create a project to disable the size class

Except for Uistackview, all other applications AutoLayout

(Align pin (pins have fixed meaning) resolve auto layout issues (solve auto layout problem

Align some of the gray above: settings for multiple controls

Leading Edge left margin (leading head

Trialling edge right margin

Top Edge Top Margin

Bottom edge Bottom margin

Horizontal Centers Horizontal Center

Vertical Centers Vertical Center

Basinlines baseline, general indication bottom

-Above, alignment for multiple controls

Horizontal in Container

Vertically in container

Add constraints Add Constraint

When the red line is displayed:

Indicates a missing key constraint

or constraint conflicts.

———————— above for align

Add width: Set in Pin

There are also four wires: constraints

Represents the distance between the control and other controls

Width: Represents the width of its own height

Height

Grey: Equal widths/equal Heights: Setting multiple controls such as width-equal-height cases

--———— above the true pin

When the Yellow line is displayed:

Indicates that the constraint and the actual position do not match

Resolve Auto layout issues (solve autoolayout problem

Selected views

Update frame

Update constraints

All views in view Controller

Update constraint just let it adapt to the current constraints

Iv. AutoLayout Setting boundaries

When using AutoLayout, it is easy to add some value or reduce some value constant will produce offset

(using third-party libraries to solve automatic layout problems can be recommended)

Use Sutolayout to stop using frame: no frame concept

AutoLayout to determine the width and x, y you just have to set the constraints, no need to calculate

V. Setting boundaries using AutoLayout

Pin: What is the tick of constant to margins?

Xcode automatically helps you to add spacing

When you set it up, remove the tick.

There is a problem when setting the spacing between the upper and lower left and right: The top margin is the distance that is set under the status bar

FIX: Click the button to select the arrow: appears: Top layout Guide/view (current distance)

Same click: Bottom layout Guide/view (current distance

What difference does it have:

Top layout Guide and Bottom layout guide

is set for the navigation controller and Tabbar

Vi. Auto Layout Sets the constraint between sibling view

Clean constant can empty the constraint

Height equal, top and bottom to its

Vii. Upper and lower constraint settings for AutoLayout

Core formula:

width of constrained view = width * Coefficient of reference view + constant

Eight, AutoLayout's tips

Set up a picture and add a constraint

1.

First add a picture and then center it, the red line disappears without setting the width height

The red line appears when you start centering but no pictures

Cause: When the picture is set, the width of the picture is used by default

2, drag and drop the small control, add the constraints of the upper and lower distance, did not set the width and height will not appear red line: The reason is that some controls have a default width high image (switch, button, TextField, although there is a default width high, but will appear with the actual yellow line, because you need to modify its width, Otherwise the default width is relatively short

3.

Nine, small application

Picture uncompressed how to set (boundary cannot be set)

Then set the center point (just set the xy of the picture) width and height depending on the picture

1X

2 x

Use 1x and put the picture in 2X picture will enlarge twice times

Get the extra cut out.

Center vertically or horizontally, and then calculate with the current position, that is, relative to the center point of the change

The center point is a relative position.

The height of the screen above is not the same, so the center point as a reference (even if the center point is changing, always a relative position

Ten, AutoLayout code implementation

Rules for adding constraints:

1) Add a constraint to the two same-level view to their parent view

2) for the constraint relationship between the two different levels of view, add to the common parent view closest to them

3) for a hierarchical relationship between the two view constraints, added to the higher-level view

Why height can be added to yourself: Because no reference is made

and other view related to the general to be added to the parent view

Code implementation

Be sure to disable the autoresizing attribute of the view (because it is a mutually exclusive relationship

Review.translatesautoresizingmaskintoconstraints=no;

Otherwise, the added constraints are not useful.

1\ Add a constraint at the top (the core formula

Nslayoutconstraint *bluetop =[nslayoutconstraint constraintwithitem://constrained view

Attribute:nslayoutattributeleft constrained Properties

Relatedby:nslayoutrelationgreaterthanorequal

Toitem: The referenced view

Attribute: Referencing View's properties

Multiplier: Multiply

Constant:20 Plus

2\ after setting the constraint, add to WHO

If there is a relationship between the child view and the parent view, it is added to the parent view

Self.view addconstraint:/addconstraints[will be used later]

3\ Adding a blueview height constraint

Nslayoutconstraint *height =[nslayoutconstraint Constraintwithitem attribute:toItem:nil attribute: Nslayoutattributenotanattribute

You need to pay attention to setting the height of the time, no reference to the view on the nil attribute transmission Nslayoutnotanattribute

Note that the right-hand incoming constant will be negative

Add to Where?

If you add it to a parent view, you can add it to yourself because there is no reference.

Xi. Add Red View

Input method: Universal: Chinese language use English mark

Attention:

1. Don't forget to close the Translatesautoresizingmaskintocontraints property

2, make sure that the relevant controls are already on their parent control

3. Do not set frame to view

Review constraints, is the reference Blueview

12. Use of the VFL language

(Visual format language)

Syntax Explanation:

@ "h:|-20-[review]-20-|"

H: Indicates horizontal direction

V: Indicates vertical direction

|-left/right boundary (distance from the left edge, 20)

[Review]

@ "V:|-20-[review (]-20-[purpleview) (==redview)"

V: Indicates vertical direction

|-20-[review (]:review) The height at the top of the distance is 20 spacing, and its height is 50

The height of the purpleview is equal to the review, with a distance of 20 Purpleview from review

When you set the width

Addconstraints: Because more than one view is set when setting a constraint

Red View and purple view are equal in horizontal direction

@ "H:[purple view (==redview)]"

Why can't ==redview*0.5 parse it?

Because: the VFL language does not support multiplication division

So if you want to achieve half the width of the situation can only be the original setup mode

Constraintswithvisualformat: Placing the VFL language

Either the top or the bottom cannot be left or right in the horizontal direction.

Can only be left and right in the vertical direction

Metrics: Dictionary @{@ "Delte": @20}

view:@{@ "Redview": Redview}] corresponds to the object that appears

Objective-c--ui Foundation Development Day (Automatic layout)

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.