A preliminary study of Size Class in IOS8

Source: Internet
Author: User
Tags compact

 A preliminary study of Size Class in IOS8Category: Ios2014-09-16 13:11 4323 People read Comments (1) favorite reports

Directory (?) [+]

A preliminary study of Size Class in IOS8

September 12, 2014

Article Directory
  1. 1. Introduction
  2. 2. Experiments
  3. 3. actual combat
    1. 3.1. modifying Constraints
    2. 3.2. Installing and uninstalling Constraints
    3. 3.3. Installing and uninstalling View
    4. 3.4. Other
  4. 4. something

Before and the classmate of Android chatting, talk about adaptation has been a very happy topic, see they are all kinds of screen adapted to torture desire to die, heart really happy for them. But when it comes to the adaptation of the IPhone and IPad, it's a headache for a page to be configured with multiple xib for development. Coupled with the release of IPhone6 and IPhone6 Plus, adaptation seems to have become cumbersome. Today I learned the Size Class in IOS8 and laughed and laughed.

Brief introduction

Let's take a look at our new partner: Size Classes. In IOS8, we don't have to create multiple xib files to fit different types of screens as we used to, and now we can put the fit of various sizes of screens into one file, and then we can customize the interface of various sizes with different categories of size. In other words, the Storyboard in front of you is not an ordinary Storyboard, but a nine-in-one Storyboard that can manage nine kinds of screens.

For width and height, there are three cases: compact, arbitrary (any), normal (Regular), so there are 9 categories, the page will be prompted when the Size class is set. For example, the width of the Compact high for any case, the hint for 3.5-inch, 4-inch, 4.7-inch of the status of the screen:

Apple's official website documents some examples, such as the IPad:

such as the IPhone:

such as IPhone6 Plus's horizontal screen status:

Experiment

The role of size class is to classify screens of different sizes, and the final layout management is AutoLayout.

Let's try a little project. The source of the project can be downloaded here.

Create a new project and go to Storyboard, and find that the default size is either width or height:

We can choose to turn the Size Class on or off in the view on the right:

Because the size class is dependent on AutoLayout, you will be reminded of the following when you try to close AutoLayout and open the size class:

Let's take a look at the next step and test what the effect would be if you threw it in directly:

Looks like a good ah, not to be used for adaptation on it?

Think too much.

Let's switch Size to Compact to see:

Oh, really good Compact!

Without manually adding Constraints, Xcode automatically assigns a default set of Constraints to ensure that you see a page of the same size as the coordinates of any size screen. This means that we can sometimes ignore automatic layouts, and no longer need to set up controls that are automatically laid out and work well, just to create Constraints for some particular views.

But now we want to keep the square centered, so we've added four Constraints to it:

Ah ha so it seems to be ... You can have it. It?

We casually changed a Size to see the effect, suddenly found just add Constraints incredibly ineffective, in the navigation bar turned gray, in the Storyboard also can not see the shadow of Constraints:

This is because our Constraints has just been set in the Compact size of the width and height, so it is not suitable for other sizes of screens. So we have to configure nine copies of Constraints in the future! This is too hard to force it! Boss, we're going to pay a raise! Obviously not, we just need to place the default Constraints in a Size with a wide height of any:

There is no problem switching to another size:

Next, let's say we want to display two labels on an iPhone device, but want to display four labels on the IPad, you can do that.

Switch size to IPhone size first, then add two labels:

Then switch Size to Regular and add three labels:

Now look at the effect on your iPhone:

Then go to the iPad to see the effect:

OK is so simple.

Actual combat

Let's take a look at how to use the Size Class to do the matching. As mentioned earlier, Size Class does not solve the adaptation problem, its function is simply to classify the screen for easy management. The actual labor of the match is still autolayout. Apple's help document gives three solutions to solve the problem of View adaptation.

We first change the project to the most original version, leaving only one view in the center of the view. The original version of the project can be downloaded here. Run a bit like this:

Modify Constraints

The first scenario for adaptation is to set different sizes of constrain for different sizes of screens.

We select a Constraint and observe its properties in the right panel:

In the right panel is the value of Constraints, the first line is the default value and applies to all dimensions. If you want to add custom values under different sizes, you can click the plus sign:

This allows you to add a custom Constraint value. Which, w and h respectively, refers to width (width) and height (height). Crefers to the Compact in the Size Class, corresponding R to the Regular, corresponding to any A .

If you want this square to hold 100 of the margin under the IPad, you can keep 0 margins under the IPhone, you can set the value of each constrant to 100, and then add a WC HA value of 0:

Run the program and look under the IPad first:

Simply perfect, and then look at the effect under the iphone4s:

Haha seems also good ... Wait, the deal is filled! How can the left and right sides empty so much space?

Suddenly think of a few days ago in the company with Xcode6 Open Project again with XCODE5 Open after some xib file will error, the effect is: Xcode6 added some Margin I do not know. Could it be these Margin in mischief? Check out the official documentation and do have a Layoutmargin attribute in iOS8. Stole PS a bit, it is true that the default value is 8 point, a false alarm, and thought it was AutoLayout bad.

But now I don't really want this Margin, how to turn it off? Click the Pin button below to remove the Margin tick:

Then re-set the Constraint, OK it finally successfully filled the entire screen:

The full source code can be downloaded here.

Installing and uninstalling Constraints

Sometimes we may encounter more complex designs that require different layouts for different sizes, which are somewhat similar to the responsive design in WEB development.

Let's say we need a View: fixed width under IPad, center aligned, and under IPhone, you want it to be centered around the left and right margins.

We just need to add top, bottom, center x, width, and in minutes we can get a layout like this:

Now we're done with the first step: fix the width under the IPad, center align.

Next we need to remove the Width property from the IPhone. When width is selected, you can see an area on the right:

It says that the current Constraint applies a screen with a wide height of any, and similar to the previous step, we can click the plus sign to add settings under different screens:

Installed in front of the tick, indicating that this Constraint is applicable to this size, if not ticked, it indicates that the size of the Constraint is invalid. For example, the following example indicates that the Constraint is valid only in cases where the width and height are Regular (i.e., the IPad):

Next we add leading and trailing to 0:

This makes it possible to align the left and right margins in the IPhone with the Center-aligned effect:

But when the IPad was turned on, it found that the fixed-width effect was not working, and became a fixed-left-to-right position like the IPhone. This is because we did not "unload" (uninstall) on the IPad screen and dropped the leading and trailing that were just set. We have two ways of solving this problem.

In the first scenario, after selecting the two Constraint of leading and trailing, the option to add a wide height of Regular to the right and remove the checkmark, indicating that this Constraint applies to all cases, is not used on screens with a wide height of Regular:

The second option, after switching to the size of the Regular Regular, select the two Constraint and then press Command+delete (note to press the Command key, or delete it completely), you can put these two Constraint in the current The Size has been uninstalled:

Run it and the IPad is no problem:

The full source code can be downloaded here.

Install and Uninstall View

Sometimes the light set Constraint is unable to meet the more complex requirements, such as the big screen I want to display three buttons, respectively: Eat breakfast, eat lunch, eat dinner. But in the IPhone and other small screen may not fit so many buttons, can only display a button: eat. In this case, we can only install and uninstall the View (uninstall).

Let's add three buttons to the View first:

But we don't want these three buttons to appear on the IPhone, so we can add the appropriate size to the right panel and remove any tick. This step is similar to the Constraint loading and unloading load in the previous chapter:

You can see that the button on the left becomes grayed out, indicating that it is not displayed under the current any size. We add a meal button, add the Regular size and remove the checkmark to indicate that you won't appear on the Regular screen:

So, in the iPhone we can see the buttons for eating:

And on the IPad, you can see the button for breakfast, lunch and dinner:

The full source code can be downloaded here.

Other

Finally, I inadvertently see something as if the left side of the Font is more:

I believe we have already pro, may as well try. You can refer to Apple's official help document for learning.

Something

First contact with Size Class, has not been applied in the actual project, there may be some understanding of the deviation, if there are errors, but also to correct, appreciate.

Along the way, feel that with the Size Class, the adaptation of IOS development work may not be imagined complex, even if the screen is bigger, we can still really laugh, laugh aloud.

    • An understanding of the AutoLayout features of iOS and the use
    • Next article Nsoperation isconcurrent

A preliminary study of Size Class in IOS8

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.