[IOS] exploring the Size Class and iosios8 in ios8.

Source: Internet
Author: User

[IOS] exploring the Size Class and iosios8 in ios8.

Address: http://blog.callmewhy.com/2014/09/12/learn-ios8-size-class/


In the past, when chatting with anzhuo, it was always a very happy topic to talk about adaptation. I was so happy to see that they were suffering from various screen adaptation. However, when iPhone and iPad are adapted, It is a headache to configure multiple xib for development on a single page. Coupled with the release of iPhone 6 and iPhone 6 plus, adaptation seems to be troublesome. After learning about the Size Class in ios8.

Introduction

Let's take a look at our new partner: Size Classes. In iOS8, we no longer need to create multiple xib files on a page to adapt to different types of screens, now we can put the adaptation of various screen sizes in one file, and then we can customize interfaces of various sizes through different types of sizes. In other words, the Storyboard in front of you is not a normal Storyboard, but a nine-in-One Storyboard that can manage nine types of screens.

For width and height, there are three situations: Compact, Any, and Regular. Therefore, there are a total of nine categories, A prompt is displayed on the page when setting the Size Class. For example, if the width is Compact and the height is Any, the screen is displayed in the portrait mode of 3.5-inch, 4-inch, and 4.7-inch:

The official Apple website provides examples, such as iPad:

For example, iPhone:

For example, the screen status of iPhone 6 plus:

Lab

Size Class is used to classify screens of different sizes, while Autolayout is used for layout management.

Let's start a small project experiment. The project source code can be downloaded here.

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

We can choose whether to enable or disable the Size Class in the view on the right:

Because the Size Class depends on Autolayout, the following message is prompted when you try to close AutoLayout and open the Size Class:

Next, let's take a look at the View and test what the result will be:

It looks good. Isn't it necessary to adapt?

Think too much.

Let's switch the Size to Compact:

Oh, it's really Compact!

If you do not manually add Constraints, Xcode automatically allocates a set of default Constraints to ensure that you can see pages of the same coordinates and sizes on any screen. This means that we can sometimes ignore automatic la S and do not need to set those controls that are both Auto-layout and have good results. Instead, we only need to create Constraints for some specific views.

But now we want to keep this Square in the center at all times, so we add four Constraints to it:

Aha, it seems like you can... You can... ?

We randomly changed the Size to see the effect, and suddenly found that the newly added Constraints is invalid, it turns gray in the navigation bar, and it cannot see the Constraints shadow in the Storyboard:

This is because our Constraints was set in the Size where the width and height are Compact, so it is not applicable to screens of other sizes. So do we need to configure nine Constraints in the future! This is too hard! Boss, we need to raise our salary! Obviously not. We only need to put the default Constraints in the Size of Any for the width and height:

Then switch to another dimension, and there will be no problem:

Next, let's assume we want to display two labels on the iPhone, but we want to display four labels on the iPad.

First switch the Size to the iPhone Size, and then add two labels:

Switch the Size to Regular and add three labels:

Check the results on the iPhone:

Go to the iPad to see the effect:

OK is so simple.

Practice

Next, let's take a look at how to use Size Class for adaptation. As mentioned above, Size Class cannot solve the adaptation problem. Its function is to classify the screen for ease of management. AutoLayout is the best way to adapt. Apple's help documentation provides three solutions to View adaptation.

First, we should change the project to the original version, leaving only one View in the center of the View. The original version of the project can be downloaded here. Run the following command:

Modify Constraints

The first method of adaptation is to set different sizes of Constrain for screens of different sizes.

Select a Constraint and observe its properties on the right panel:

The right panel is the value of Constraints. The first line is the default value and applies to all sizes. To add custom values of different sizes, click the plus sign:

In this way, you can add a custom Constraint value. Where,wAndhWidth and height respectively ).CCompact in Size Class,RCorresponding to Regular,ACorresponds to Any.

If you want this square to keep a 100 margin under the iPad and a 0 margin under the iPhone, you can set the value of each Constrant to 100, then add a wC hA value of 0:

Run the program. The first step is iPad:

It's perfect. Let's look at the effects of iPhone 4S:

Haha seems good too... Wait, let's fill it up! Why are there so many blank spaces on both sides?

I suddenly remembered that some xib files reported an error when I opened a project with Xcode6 and used Xcode5 in the company a few days ago. The general idea is that Xcode6 has some Margin, which I don't know. Will these Margin be playing tricks? I checked the official documentation and found that there is indeed a new layoutMargin attribute in ios8. I secretly took a bit of PS, but the default value is 8 points. I was shocked and thought AutoLayout was broken.

But now I don't really want this Margin. How can I turn it off? Click the Pin button below to remove the Margin check box:

Then re-set the Constraint. OK, it finally fills up the entire screen:

Complete source code can be downloaded here.

Install and uninstall Constraints

Sometimes we may encounter complicated designs that require different la s for different sizes, which is quite similar to responsive designs in Web development.

Suppose we need a View: fixed width and center alignment under the iPad. Under the iPhone, we want it to keep the center alignment between the left and right margins.

We only need to add top, bottom, center x, and width to create a layout in minutes:

Now we have completed the first step: fixed the width and center alignment under the iPad.

Next, we need to delete the width attribute in the iPhone. After selecting width, You can see such an area on the right side:

It indicates that the current Constraint applies to the screen with Any width and height. Similar to the previous step, we can click the plus sign to add settings under different screens:

Check the front of the installed to indicate that the Constraint is applicable to this dimension. If it is not checked, it indicates that the Constraint is invalid under that size. For example, the following example indicates that this Constraint is valid only when the width and height are Regular (that is, iPad:

Add leading and trailing to 0:

In this way, the iPhone is able to align its left and right margins in the center:

However, after you open the iPad, you will find that the originally set fixed width is invalid, which is the same as that of the iPhone. This is because the leading and trailing we just set are not "uninstalled" (uninstall) on the iPad screen. There are two ways to solve this problem.

Solution 1: After selecting the two Constraint leading and trailing, add the option that the width and height are Regular on the right side and deselect the check box, indicating that this Constraint applies to all situations, do not use it on the screen where the width and height are Regular:

Solution 2: switch to the Regular size, select the two Constraint, and then press Command + Delete (note that you need to press the Command key, otherwise it will be completely deleted ), you can unmount these two Constraint Values in the current Size:

Run it, and the iPad is OK:

Complete source code can be downloaded here.

Install and uninstall View

Sometimes it is difficult to set a Constraint to meet complicated requirements. For example, in the big screen, I want to display three buttons, which correspond to breakfast, lunch, and dinner. However, there may be no more buttons in small screens such as iPhone, and only one button can be displayed: Dinner. In this case, we can only install and uninstall the View ).

Add three buttons to The View:

However, we do not want these three buttons to appear on the iPhone, so we can add the appropriate dimensions on the right panel and remove the Any check box. This step is similar to the installation and uninstallation of Constraint in the previous chapter:

The Button on the left is dimmed, indicating that the Button will not be displayed under the current Any size. Let's add another meal button, add the Regular size, and deselect the check box, indicating that you will not appear on the Regular screen:

In this way, we can see the meal button in the iPhone:

On the iPad, you can see the buttons for breakfast, lunch, and dinner:

Complete source code can be downloaded here.

Others

Finally, I accidentally saw something more on the left of Font:

I believe you are already familiar with this. Try it. For more information, see the help documentation provided by Apple.

Remarks

The first time I came into contact with the Size Class, I have not applied it in the actual project. I may have some misunderstanding. If there is any error, I hope to correct it. I am very grateful.

Along the way, I feel that with the Size Class, the adaptation work for iOS development may not be as complicated as I imagined. Even if the screen is bigger, we can still laugh and laugh.

References:

  • IOS8 Constraints and Size Classes
  • What's New in iOS8
  • Size Classes Design Help
  • Auto Layout In iOS 8-Layout Margins
  • Layout attributes relative to the layout margin on iOS versions prior to 8.0

The property problem in Objective-C class in iOS Programming

The first method also contains @ sythesize name in implementation. This method is actually the abbreviation of @ sythesize name = name. @ sythesize XXX = YYY means, the getter and setter access functions are automatically generated for the instance variable YYY of the class. The generated function declaration is getXXX () and setXXX (xxx ). In addition, you can use a simple. Operator to access and assign values, which is equivalent to an access function.

How does ios71 upgrade ios8?

Ios7 is so painful that I can try Nima again... Purely cannon fodder
 

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.