Turn////ios 8 Auto layout Interface automatic layouts Series 1-Fundamentals of automatic layout

Source: Internet
Author: User
Tags dashed line set background vars

IOS 8 Auto Layout Interface Series 1-Fundamentals of automatic layout

http://blog.csdn.net/pucker/article/details/41832939

Tags: iosinterface apple layout Interface2014-12-10 00:25 11286 People read Comments (2) favorite reports Classification:iOS Development (+)

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

Apple has released a new iphone 6 and iOS 8 system this year, and the SDK has made several improvements to the interface adaptation of the new device and system, so I would like to take this opportunity to share my own understanding of the automatic interface layout, if there are errors please feel free.

First, the basic principle of view positioning

A view is a rectangular area that is used to display content, which is the most basic unit of an app's interface. As a cross-screen interface for the Calculator app, each calculator button is a view.

In a planar Cartesian coordinate system, the exact description of a rectangle requires determining the following four layout properties (layouts Attribute), i.e. horizontal position x, vertical position y, width w, height h (no rotation considered) . This view can be added to the interface correctly only if the above 4 layout properties are explicit.

For example, if you want to add the number key 5 to the interface, the code looks like this:

[OBJC]View Plaincopy
    1. uiview* number5View = [[UIView alloc] initwithframe:cgrectmake (x, Y, W, h)];
    2. [Parentview addsubview:number5View];

The code above gives a detailed view of the values of the 4 layout properties, which we would like to call "Explicit coordinate positioning mode".

Second, the old interface layout method and its shortcomings

Consider the following questions:

    • What is the problem with the explicit coordinate positioning method when working with a large number of views?
    • What is the problem with the explicit coordinate positioning method when dealing with the rotation of the screen?

First, when the number of views is large, it is tedious and error-prone to build each view because each view needs to give a clear 4 attributes. Also, you need to recalculate the new layout property values for each view when there is an interface change, such as a portrait-to-landscape rotation. This is clearly not a satisfactory solution.

So Apple gives a better solution, which is to create a view with its "alignment and scaling factor" relative to the parent view, which is autoresizingmask. When the parent view changes, the new location is automatically drawn through the autoresizingmask of each child view without the developer's offer. For example, in the Equals key, you can specify that its alignment is right-aligned, bottom-aligned, and that its scaling factor is fixed-width, fixed-height. This guarantees that the equals key is always in the lower-right corner of its parent view and has a wide-height constant.

Apple solves the problem in a correct way, by giving the rules of the interface changes to automatically calculate the values of the layout attributes , thus eliminating the developer's work.

The problem with Autoresizingmask, however, is:

    • Its description interface change rules are not flexible enough, many change rules simply can not be accurately described. The Autoresizingmask scaling is calculated internally by Uikit, and the developer cannot specify the exact value of the zoom scale.
    • Change rules can only be based on the parent and child views, and cannot establish a relationship between a sibling view or a cross-level view. For example, for the calculator, Autoresizingmask cannot describe the number key 5 is close to the number key 8 below, as well as the number key 0 to the left of the number key 1 to the left and the number key 0 right and the number key 2 right alignment such rules.

So in the past, a lot of times developers have to bite the bullet to use explicit coordinate positioning to solve the screen (such as overriding the Layoutsubviews method), or to do a horizontal screen and vertical screen two sets of interface.

These developers can endure, but the nightmare has just begun ...

iphone 4s and the early iphone screen size are 320*480, basically running on the test machine is the effect of user operation. But since the iphone 5 has brought 16:9 widescreen, the size has become 320*568, and other apps need to support the ipad. Do all of these devices require developers to handle the exact location of each view themselves and handle the turn screen themselves?

Apple is aware of this problem, so when iOS 6 was released, it introduced the concept of automatic layout and layout constraints (Constraint) to solve the problems encountered.

Three, the principle of automatic layout and the use of the method

Automatic layout is a further refinement of the Autoresizingmask, which allows developers to establish precise rules of linear variation between any of the two views on an interface. The so-called linear change is a function of mathematics, namely:

y = m*x + C

where x and Y are a layout property of any two views in the interface, M is a scale factor and C is a constant. For example, if we want to describe the number key 5 sticking to the number key 8 below, we can establish the following rules:

    • Number Key 5 Horizontal center X coordinate = 1.0 * Numeric key 8 Horizontal center x coordinate + 0.0//8 and 5 Horizontal center alignment
    • The top y coordinate of the number key 5 = 1.0 * Number key 8 The bottom y coordinate + 0.0//8 The top of the 5
    • Width of number key 5 W = 1.0 * Number Key 8 width W + 0.0//8 and 5 width equal
    • Number Key 5 Height h = 1.0 * Number key 8 height H + 0.0//8 and 5 height equal

Each linear change rule is called a layout constraint (Constraint). Because each view needs to determine 4 layout properties for accurate positioning, it is generally necessary to establish 4 layout constraints.

In the next article, I'll explain in detail how to add automatic layout constraints, so stay tuned.

I

OS 8 Auto Layout Interface Series 2-add layout constraints using Xcode's Interface builderTags: iOS apple interface Iphonexcode2014-12-11 10:37 31179 people read review (+) collection report Classification:iOS Development (+)

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

The previous article, "iOS 8 interface auto-layout series-1", gives a brief introduction to the way the iOS interface is laid out in the past life. This article details how to use automatic layouts to adapt to different screen sizes.

There are three ways to add automatic layout constraints (hereinafter referred to as constraints):

    • Adding and setting constraints using the Interface Builder Interface designer of Xcode
    • Add a constraint by code article by clause
    • Adding constraints through the Visual format language VFL

This article will show you how to add constraints in these ways, as shown in a simple example. The interface of the program mainly consists of three parts, the top is a company logo image, the following is the area can be scrolled, including the company name and detailed description.

Open Xcode (This article uses the version 6.1.1), create a new project, and select Single View application, iOS, application.

The project is named Autolayoutxcode, language selection objective-c, device Selection Universal.


Click Main.storyboard, drag an image view, a scroll view, and two labels, and use the two label as the ScrollView child view. To make it easier to identify, change the name of the image view to the logo image, and the name of the two label to Description label.

Download the Apple logo image apple.jpg and drag it into the project. File: Http://yunpan.cn/cfmJB82dfSwf6 (Extract code: 4049)

With the logo image selected, in the image view of the Properties window (Attributes Inspector), set the image to apple.jpg and mode to aspect Fit. This allows you to adjust the display of the image according to the size of the image view.

In order to correctly position the image View, we need to add 4 constraints, namely:

    • The left side of the Image view is aligned to the left of the parent view
    • Right side of Image view aligned to the right side of the parent view
    • The top of the Image view is aligned with the top of the parent view
    • The height of the Image view is half the height of the parent view

Then select the Logo Image, click on the Pin menu below the designer, uncheck constrain to margins (otherwise there will be spacing) above the pop-up menu, and set the left, right and top constraint values to 0 (Note to light the constraint mark from the dashed line to solid), Finally, click the Add 3 Constraints button below the pop-up menu to add the above three alignment constraints, as shown in.

Size inspector to see the dimensions of the currently selected interface element and the constraints that are added. Select the logo image and click Size Inspector to see the three constraints you just added, as shown in.

The three blue lines represent the left, top, and right alignment constraints, respectively. The list below also lists these three constraints in detail, Trailing Space to ... For right alignment, leading Space to ... Left-justified, top Space to ... Aligns to the top. The mouse double-click on the top Space to:superview constraint displays the details of the constraint, as shown in.

Recall the previous article on the introduction of constraints, constraints are a linear relationship:

y = m * x + C

The vertical Space constraint in the figure indicates that the current constraint is a vertical spacing constraint. The first item corresponds to Y in the formula, which represents the dependent variable, which is shown here is the logo image.top represents the top of the picture. The relation corresponds to the = in the formula, which represents the equality relationship, which shows that equal is equal. The Second item corresponds to the X in the formula, which represents the argument, which shows that Superview.top represents the top of its parent view. The multiplier corresponds to the m in the announcement, which represents the scaling factor, where the value is 1. The constant corresponds to the C in the announcement, which represents the offset constant, where the value is 0. So a complete string of images is clearly expressed in relation to the top of the parent view.

Note that the two sides of the constraint are equal, either that Y changes relative to x, or that x changes relative to Y, so that the first item is interchangeable with the second item, which is the inverse function:

x = 1/m * y-c/m

Click on the first item or the Second item drop-down menu and select reverse First and Second item to swap the positions of both sides.

The multiplier and constant after exchange are calculated automatically based on the inverse function formula.

Notice that a red button appears in the upper-right corner of the document structure window on the left, indicating that there is a layout constraint error in the current interface. Outline

Click this button and Xcode will list the errors and give the suggestions for resolution.

We now add only 3 constraints to the picture, and we lack the constraint to determine the bottom position of the picture, which is the missing height constraint (need constraints for:height) as indicated in the figure. Click the red button and Xcode will give you a solution.

At this point, if you click the Add Missing Constraints button, Xcode will add the missing constraint itself, but it may not be as we wish or even cause confusion. So we click Cancel and we add the constraint ourselves.

We want the picture to always occupy the upper half of the screen, so you can create a height constraint between the picture and the parent view. In the Document Structure window, click to select Logo Image, control-Drag to its parent view view, and select Equal Height in the popup menu.

To keep the logo image selected, double-click the equal Height to:superview constraint in the Size inspector window and modify the multiplier value to 0.5.

The red arrow at the top right of the document structure window changes to a yellow arrow, indicating that the constraint has a warning rather than an error. A misplaced view appears after clicking on the yellow arrow, indicating that the current layout state of some views in the designer is not the final state after the constraint has been applied, and that the location needs to be updated according to the constraint's requirements. Click the yellow triangle button, select the update Frame in the pop-up window, and click the Fix misplacement button.

The scroll view is then created with a constraint that is left-aligned, right-aligned, and bottom-aligned with the parent view, and the vertical spacing between the image view and the scroll view is 0. In addition to using the Pin menu, you can create constraints directly in the Document structure window or in the designer by dragging and dropping. In the Document Structure window, select scroll view, control-drag to its parent view view, hold down the SHIFT key and the option key in the pop-up window, check leading space to Container, Trailing space to Container and Bottom Space to Bottom Layout Guide, click outside the window to close the window.

Select the logo Image in the designer, control-drag to scroll view, and vertical Spacing in the pop-up window.

The interface designer can display constraints on the selected view in real time. When scroll view is selected, 4 blue lines are displayed around it, with each blue line being a constraint.

By dragging and setting constraints, Xcode determines the constant value of the constraint according to the current actual size of the view, so it needs to be modified manually. In the Size Inspector window, click the Edit button for each constraint, and change the value of constant to 0.

Now the scroll view constraint is added. We can then add a constraint to the name label in the same way, aligning its left, top, and right sides to the corresponding portion of the scroll view, and qualifying its height at 20.

After clicking Add 4 Constraints, the interface designer displays 4 Orange Line lines that represent the current position size mismatch constraint for the view, and you need to update the location dimensions based on constraints. Click the Resolve Auto Layout issues button below the interface designer and click Update Frames to update the location size.

With the name label selected, set the text of the label in the attribute inspector window to "Apple" and set the background to green.

Also adds a constraint to the description label, aligns its left, bottom, and right sides with the corresponding portion of the scroll view, and the top is equal to the bottom of the name label. In the Attribute Inspector window, set the text of the label to

"Apple Inc. is a high-tech company in the United States. Three people, including Steve Jobs, Steve Wozniak and Rod Wehn (Ron Wayne), were founded on April 1, 1976 and named Apple Computer Inc., the Apple Company, January 9, 2007, Headquartered in Cupertino, California State.

The PC, which was primarily developed and marketed at the beginning of Apple, was dedicated to the design, development and sale of consumer electronics, computer software, online services and personal computers as of 2014. Apple II in the 1970 helped the PC Revolution, and the subsequent Macintosh relay continued in the 1980. The company's hardware products are mainly Mac computer series, ipod Media Player, iphone smartphone and ipad tablet, online services include icloud, ITunes store and App Store, consumer software includes OS X and iOS operating system, itunes Multimedia browser, Safari web browser, and ilife and iwork creative and productivity suites. Apple is known worldwide for its innovation in high-tech companies.
Apple's December 12, 1980 IPO, which hit a $623.5 billion market record in 2012, has been the world's largest market capitalisation for three consecutive years as of June 2014. Apple ranked 15th in the world's top 500 rankings in 2014. Apple overtook Coca-Cola as the world's most valuable brand in the "Global Best Brands" Report of the Hong League group on September 30, 2013. The Apple brand overtook Google in 2014 to become the world's most valuable brand. ”

Set the value of lines to 0, set background to yellow, and update the position size.

We can preview the interface at any time using Xcode's preview tool. Open Assistant Editor and select Preview----Main.storyboard in the Quick Jump drop-down menu.

In the right-hand preview window, you can see the actual performance on different devices. You can click the + button in the lower left corner of the preview window to add a device that needs to be previewed in the pop-up menu.

Now it's time to actually run the program in the simulator and find that scroll view has turned sideways rather than vertically, which is not the effect we want.

Note that when you use automatic layout to add constraints to scroll view, you need to specify the contentsize size of the ScrollView in addition to the location dimensions of the ScrollView itself. For the above example, Scroll view itself occupies the lower half of the screen, but the size of its contentsize is indeterminate, so an abnormal scrolling effect occurs. So we also need to determine the width of the two labels so that we can calculate the contentsize. Press the command key to select the logo Image, Name label and description label in the Document Structure window, click the Pin button and select equal width in the popup window, so that the width of the two Label is always the same as the image The width of the view is equal, i.e. the width of the contentsize of the scroll view is always the same as the width of the scroll view itself, so there is no horizontal scrolling.

Run again, now only scrolls vertically. Program final project File Link: Http://yunpan.cn/cfu9J9qvUQz2P (extract code: 7654)

With this example, it is not difficult to find out, in fact, the concept of automatic layout is not difficult, and it can be a good solution to multi-device interface adaptation and screen rotation processing work. And in these kinds of ways to add constraints, using Xcode's Interface Builder to visually create constraints and use the Preview tool to adjust constraints in real time is the simplest and most common way.

In the next article, we'll focus on creating Nslayoutconstraint objects through code, and how to add constraints using the VFL language.

Turn////ios 8 Auto layout Interface automatic layouts Series 1-Fundamentals of automatic 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.