Automatically layout autolayout and sizeclass in xcode6

Source: Internet
Author: User
1. autolayout)


In xcode, automatic layout seems to be a complicated system. I thought so before using it, but that is not the case.

We know that the main UI component of an iOS app is composed of relatively independent visual units, which are mainly responsible

Outputs useful information, and some are responsible for the input (interaction) of information. The interaction process is often accompanied by the animation effect, which has reached the connection of the entire information transmission.

Experience and user experience. Visual units are mainly view and button in actual development. The relationship between these visual units consists of two basic

Relationship Structure: sibling and parent-child relationships. The entire view unit is a tree structure:



For any UI component, the position and size of the component (relative to the parent view) determine the Display Effect of the component in the UI view.


Autolayout (and the new sizeclass in ios8.) is used to solve the problem of layout and arrangement of these UI visual units or elements. In the past

When only iPhone 4 is available, we can write no visible unit in the code, which is okay, but with the release of iPhone 5 and 6,

More and more screen sizes may be available. In the future, more iPhone sizes will not be excluded, which requires that the UI elements of our app have the size on the screen.

Different devices have dynamic tonality and have achieved better UI display. From the current technology provided by Apple, there are three implementations: Bottom, middle, and top.

Method:


The next step is to determine the size of the current device in the Code and manually adjust the UI elements. The disadvantage is that the Code is complex, error-prone, and

Great maintenance difficulty and poor flexibility;


The policy is to set the Visual Unit (uiview uibutton ...) the autoresizing attribute of. It specifies how to adjust the size and position of a view when its environment (parent view) changes, this method can be set in xcode interface Builder (storyboard or XIB), but it can only be adjusted for the parent-child relationship, such as whether the left margin is fixed or the size is variable, the adjustment of sibling relationships cannot be achieved, and the adjustment method of apps with relatively fixed UI is basically enough;

The best strategy is to use autolayout and sizeclass to comprehensively adjust the parent-child relationship and sibling relationship of the UI Visual Unit, and the adjustment accuracy is higher: not only can we determine the basis for changing the position and size of a view, but we can also set different priorities for the basis, which conditions are met first and then what conditions are met, priority can be ensured for important positions and sizes, so that the entire app has strong dynamic tonality and can meet the needs of different devices and different application scenarios.


When the size of an Apple mobile phone is as high as four, it is clear that the new app must adopt the best strategy to solve the layout problem of view components.


Autolayout has a clear role: it helps us determine the proper position and size of the same Visual Unit under different devices and different (parent view) environments. Therefore, when the constraint applied on a uiview uniquely determines its frame (X, Y, width, height), our automatic layout is correct. The two types of errors that beginners usually make are insufficient constraints (too few constraints) and constraint conflicts (too many constraints ). If the constraints you give can only determine the size of the view, or an item in the position or position (such as x), there will be insufficient constraints, in the XIB or storyboard, a yellow warning appears in the prompt box on the left. If you export two or more conflicting positions and sizes according to the constraints, A Layout error occurs, but the build fails during compilation.

Ii. sizeclass attribute added by ios8


In ios8, the size classes feature is added, which is an abstraction of the current size of all IOS devices.

OS-size devices: iPhone4-5-6-6plus, iPad, iPad Mini, iWatch, how to write different according to the previous specific device

Layout is definitely a bad thing. Now that we have sizeclass, we can handle a lot of things: Aren't you equipped with many devices, so we just need to set the screen width

And high are divided into three types: compact, regular, and any, that is, compact, normal, and arbitrary. In this way, the integration is wide and comprehensive, with a total of 9 Cases

. As shown in, we can set the Automatic Layout constraint of uiview in storyboard or XIB separately if needed, or even

Whether a button is displayed can be easily implemented.



For detailed analysis of size class, refer to the Apple documentation and wwdc2014: Click the open link (What's New in interface builder ).

Iii. invincible combination of storyboard autolayout and size class

Kids shoes that are familiar with xcode's interface builder should have a deep image of uibutton's strong customization: by selecting different states of the button (normal, height, disabled ...), we can set each State separately, such as the background image, image, and text color of the button.

See:


The major changes to Automatic Layout in xcode6 are similar: developers can refer to one of the nine combinations of size class according to their actual needs.

In this way, when the app runs on different screens and different rotation directions, you can set the layout based on the current environment

Size class uses the layout information we have set in advance to achieve great flexibility in the app UI.

Similar to setting different attributes of uibutton in different states, we first select a size class and then perform automatic layout for this size class. The following uses a simple layout scenario as an example:

Suppose we want to achieve the following effect: horizontal screen and portrait pictures and labels can both be normal, and in the "more appropriate" position: Obviously horizontal screen

(Height: compact), we need to add a (wany, hcompact) method to the normal layout first)

Size Class layout:

First, we will deploy the default sizeclass to determine the position and size of the Avatar and label:




After wany Hany is set, click wany Hany text (bottom) and select wany hcompact.

The size class of the response can be selected by dragging the mouse. Note that in the lower right corner (indicated by the red box), you can also choose whether to install.

If this option is deselected, the Avatar will not be loaded under the current size class (naturally, it will not be displayed ).




In the new size class, we start to add a new layout. Note that the layout is not covered in the previous size class. We have defined the layout, knowledge adds new and independent layout information for the current size class. Here, xcode6 has given us nine holes!



After the layout is complete, run it to achieve the desired effect!

Awesome, isn' t it?


Automatically layout autolayout and sizeclass in xcode6

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.