iOS Interface Adaptation II: Use of Auto layout (top)

Source: Internet
Author: User


The previous section on iOS based on the distance units, linked to the iOS interface for a: PT and px relationship, using distance units can define the size of the view in our program, but only using PT to define the size is far from enough.

For example, we want to have a button from the left edge of the phone to 20pt, the right side bounded by 20pt, the distance is 30pt, the height itself is 20pt, if in iphone5 (for 320pt*568pt), we want to define in the code so

The following code is written in the Viewdidload method in Viewcontroller:

    UIButton * Button  =  [[UIButton Alloc]initwithframe:cgrectmake (+, 280, +)];        [Button settitle:@ "Hello pt" forstate:uicontrolstatenormal];        [Button Setbackgroundcolor:[uicolor Graycolor];        [Self.view Addsubview:button];

Because Iphone5 is 320pt, the width of this button must be 280pt good, look at the effect:


Well, it seems to be in line with the requirements, turn around and try it:

Wait, how is it different from the idea? This is because, when we set the button, we only consider the Iphone5, the size of the horizontal screen, the iOS traditional method in judging the view position, the view in the parent view of the location, the view itself is the size, so, If the button is at the parent view's (20,30) size (280,20) When you press the condition we just started, the parent view changes when we cross the screen, but the button does not change in the parent view and the size does not change.

In another case, what would happen if we switched to a different resolution of the phone:

As can be seen, under the Iphone6 is completely wrong, this is because the size of the parent view has changed, according to the size of iphone5 will certainly be wrong


The above mentioned is the traditional method of restricting view location and size, it should be noted that:

when we open a layout file and drag it into the control, or when we use the initWithFrame method in our code, we use the default traditional restriction method, which is the method above

Although this method is simple, easy to understand, but the use of the limitations are very large, almost can only be used when the iphone resolution is not changed (when many projects do not use layout files, only pure code write), but now the iphone resolution of up to 4 kinds, And sometimes asked to support horizontal screen, using the above method can be said to be difficult, the following we use auto layout to solve the above problem


I. Using auto layout in a layout file

Let's take a look at Apple's official explanation for auto layout:

AutoLayout is a constraint-based, descriptive layout system.

Key Words:

    • Based on constraints-and the position and size of the previously defined frame, the position of the AutoLayout is defined by the so-called relative position constraints, such as the x-coordinate of the center of Superview, the y-coordinate is 10 pixels above the bottom of the screen, etc.
    • Descriptive-constraint definitions and individual view relationships are described using approaches that are close to natural languages or visual languages
    • The layout system-that is, the literal meaning, is responsible for the position of each element of the interface.

In summary, AutoLayout provides developers with a layout method that differs from the traditional placement of UI element locations. Previously, whether you were dragging or dropping in IB or writing in code, each uiview had its own frame property to define its position and size in the current view. With AutoLayout, it becomes the use of constraints to define the location and dimensions of the view. The biggest benefit is the solution of the different resolution and screen size of the view of the adaptation problem, but also simplifies the rotation of the definition of the view position , the original 10 pixels above the bottom of the view, whether in the rotation of the screen or change the device, The position is always centered 10 pixels above the bottom and does not change. In summary, using constraints to describe the layout, the view frame is calculated based on these constraints.

Part of the above excerpt from: AutoLayout (Automatic layout) Getting Started

Here's my personal insight into auto layout:

1. When you use auto layout for a Viewa, the system will only use the conditions in auto layout to limit Viewa, and the traditional setting frame or size you originally made to this viewa will be invalid immediately.

2.Auto layout is closer to our natural language, which is a tool that is closer to our actual needs

3.Auto layout is based on constraints, in general, a view requires at least 4 constraints to determine its location and size

Here we use auto layout to solve the above problem:

Constraint use method;

1. Drag in a button, CTRL + Left on the button, and drag onto the main view

or in document outline on the left.


2. Let go, the popup box appears:

Where leading Space to Container margin is the distance from its left edge

Trailing Space to Container margin is a distance from its right border

Top Space to top Layout guide is a distance from its upper boundary

Buttom Space to Buttom Layout Guide is a distance from its lower boundary

Set its size to do this:

Fill the constraint width in its width, and fill in the height

3. After adding the constraint, change the values of each constraint:

OK, we set this button to the left edge of the screen is 20pt, the right border is 20pt, the distance is 30pt, the height of itself is 20pt (from here can be seen that the constraints are really very consistent with the actual problem), to see the effect:

Iphone6 Screen:

ipad Air Screen:

Very good, consistent with expectations, and very simple.

So, what is the principle of constraint?

In fact, the constraint is almost the following formula:

A = B * m + C


Or


Viewa.attribute = Viewb.attributs*multiplier + constant

We look at the left-bound constraint in the graph:

The four boxes on the right correspond to a, B, C, m for a = b * m + C expressions, respectively

Converted to a language, the left side of the button is the left edge of the screen multiplied by a 20pt, and our problem is the same

So, to join us, we want the top of the button to be three-fourths at the bottom of the main view, you can set the multiplier in the top of the button to the bottom of the main view as 0.75,constant 0

Note that you must first remove the button from the top of the distance, otherwise there will be too many constraints causing confusion

Method:

To add a buttom Space to buttom Layout guide limit, multiplier to 0.75,constant 0, remember to Change to Button.top because the default is Button.bottom:

Run to see the effect without any problems

Using the layout file to use Auto layout is the first to say this, in fact, there is a lot of learning, there is time to write out later.

Using only the layout file to use auto layout is still a bit inadequate, because it is written dead, sometimes not enough to meet the requirements, we can also dynamically change the layout file constraints in the code constant, or dynamically add constraints, which will be written in the next article



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

iOS Interface Adaptation II: Use of Auto layout (top)

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.