IOS 8 by Tutorials (section2: Start adaptive layout)

Source: Internet
Author: User

 IOS 8 by Tutorials (Translator 1.2)1.2. Start self-adapting layout

iOS8 's huge changes, Adaptive Layout the references to iOS designers bring help. When designing a project, you can create a simple layout to fit the current iOS8 device, and this chapter is an Adaptive Layout introduction to general-purpose, Storyboards size classes layout and custom fonts, and a super-useful assistant Edit preview (storyboards clearly see the project structure). Now you can create a project from scratch, if you're not Auto Layout a fan, it doesn't matter, the first part of this chapter provides a step-by-step approach to building a project using automatic layouts, and you'll find that you can do a lot of things without using a single line of code.

    • A common story version

The general version of the story will be your Auto Layout first step in the journey, the same on storyboard iOS8 can be used for ipads and iphones, it does not need to maintain storyboard a set of services can only synchronize a device, and is full of errors. Build a Single View Application project, tap Main.storyboard , it is a single storyboard but compatible with all devices regardless of the device screen size, it contains a simple view controller , but really an unfamiliar size:

Yes, it is a "rough square" and in the File Inspector selection Use Size Classes , you can achieve "one story version fits all":

This option is available by default for new projects in IOS8, and you can also use this new version of the story when updating your project. You can Adaptive View Controller Hierarchies learn how to update the story version in IOS8 in the 4th section of this chapter.

So start, open Main.storyboard , drag one on the ImageView interface, Size Inspector set it in the X position for, the position for, for 150 Y 20 Width 300 , Height for 265 . Drag one on the View interface, Size Inspector set it in the X position for, the position for, for 150 Y 315 Width 300 , Height for 265 .

Select the View open Identity Inspector bar You just added TextContainer in your Label box. Such as:

It's hard to see where you just added it View because it and the background color are white. In order to differentiate them, now set the background color for Red: 74, Green: 171, Blue: 247 , TextContainer view the background color for Red: 55, Green: 128, Blue: 186.  , well, now see is this:

Now that there are 2 views in our view, the following task is to set the layout constraints on them.

    • Add a layout constraint condition

      Select ImageView, click on the auto layout toolbar Align button, select Horizontal Center in Container , make sure its value is 0 , then click the Pin button and add one top spacing to nearest neighbor constraint for 0 , for example:

The constraint has just ensured that the distance from the top of the ImageView is fixed and centered on the left and right. Now we need to configure the spacing between ImageView and TextContainer. Ctrl-dragImageView to Ctrl-drag, like this:

There will be one menu , select Vertical Spacing , like this:

This constraint determines the vertical spacing between the bottom of the ImageView and the top of the Textcontainerview. Select Size Inspector , then select ImageView you can see this:

You can see that there are 3 constraints on the ImageView, and you can size inspector easily configure each of these constraints. In Bottom Space To: TextContainer the constraints click Edit , you can set the properties of this constraint, set Constant to 20 , for example:

Well, now we have achieved the textcontainerview fixed at the bottom of the ImageView 20px. But we still need to add the other three constraints on Textcontainerview: Select Textcontainerview, click Pin to display the Add New Constraints interface, select in, Spacing to nearest neighbor left right bottom value to 0, make sure Constrain to margins yes unchecked , this means to remove all margins from this interface (padding to left and right), click Add 3 constraints , add a new 3 constraints to Textcontainerview,

Now our version of the story is this:

This time you will notice that there are some yellow constraint lines on the interface, which indicates that these constraints are problematic and need your attention. It will follow the constraints when the story version is automatically updated, but you can choose to automatically update your imageview to zoom out zero size . This is because your imageview does not have any content-meaning that its interior is both high and wide 0 (requires 4-direction constraints), and that if you do not provide a physical (picture) height limit Auto Layout depends on the internal size of the interface to determine the width of the height. In the Project Navigator , open the Images.xcassets Select + button below New Image Set :

Double-click the just-added Image and rename to cloud , such as:

This cloud is a collection of pictures, contains,,, non-Retina Retina Retina HD as long as in the project to determine the existence of the picture, then when you refer to the name of the image, iOS can runtime get the picture resources. Now that the cloud picture set is empty, you can download it later, drag it in, drag it in, and drag it into it cloud_images.zip cloud_small.png 1x [email protected] 2x [email protected] 3x , such as:

Select 3x , click on the keyboard space keys to see [email protected] the prototype diagram:

After adding the picture resource, click and Main.storyboard Select ImageView, set to, Image set as cloud View\Mode Aspect Fit , for example:

Now the picture also has, but still can see some yellow constraint line, we still need to do something: first select View Controller‘s the view, select Resolve Auto Layout Issues , the last choice All Views\Update Frame :

This time the View Controller interface will be rearranged to automatically resolve the unsatisfied constraints, which will make our version of the story look like this:

    • Preview Assistant Editor

      Under normal circumstances we test our General story version: iPad, iPhone 4s, 5s, 6 and 6 Plus run on the emulator separately, which also tests the direction. This process is undoubtedly laborious, but Xcode 6 using it Preview Assistant Editor (Preview Assistant editor) is a great experience!

Open the Main.storyboard Select Assistant Editor menu bar:

This time the story version will be divided into 2 pieces, select----- Automatic Preview Main.storyboard :

Preview Assistant Editor will appear another 4-inch(iPhone) version of the story:

In the iPhone 4-inch click the Rotate button at the bottom of the interface, the interface will be displayed horizontally:

In Multiple simulator displays, this is a huge change, but wait, there's so much more! Click + you will find more (awesome): (October 26, 2014 1:16)

Add iPhone 5.5-inch and iPad , now the interface is like this:

Do you find it a little strange to have a horizontal screen? Right! Because the picture is too big. You now need to add a new constraint to the ImageView. Ctrl-dragImageView to controller’s view create a new constraint, select Equal Heights :

Now there are some red constraints on the interface. This is because there is a conflict between the constraint that you just added and the existence. Select just the constraint, select Document Outline open Attributes Inspector , if First Item not cloud.Height , then in the First Item selection Reverse First and Second Item :

Next select Relation as Less Than or Equal , set Multiplier to 0.4 :

This means that this picture either keeps the size of the image itself or the 40% height of the screen size, taking the minimum value. You will notice that the interface refreshes automatically after you update the constraint:

Can you get a preview of multiple devices after Automatic Updates? Of course! Preview Assistant editing is a pretty progressive thing! Somewhere between this is a weather project, we need to add some lable to represent the city name and the current temperature.

    • Add text information

      Main.storyboard TextContainer Add 2 in the Labels :

Select the label above to use Align and Pin set horizontally center top spacing to nearest neighbor the value for 10 :

Next choose the Attributes Inspector menu bar, set the label to, for, Text Cupertino Color White font Helvetica Neue, Thin size: 150 , then you may find the current text is difficult to distinguish, this is because the label frame problem, You can solve it quickly.

Choose another label, the same use Align and Pin settings horizontally center , bottom spacing to nearest neighbor The value is 10 , select the Size Inspector menu bar, the interface is like this:

Use the Attributes Inspector menu bar to set the label to, Text for, 28C Color White font Helvetica Neue, Thin , font size:250

Now it's time to address the frame problem of the label mentioned earlier. Select view controller’s view , click storyboard below Resolve Auto Layout Issues , then select All Views\Update Frames , and you will see storyboard this is the case:

2 labels a little overlap, that's not what you want. Take a look at it before we fix it assistant editor , and it looks perfect on the ipad:

It is conceivable that the font on the iphone is slightly larger:

OK, you will be in the next section to solve the font size problem.

November 10, 2014 17:31

IOS 8 by Tutorials (section2: Start adaptive 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.