Size Classes with Xcode 6: Prepare a storyboard for all sizes

Source: Internet
Author: User

My favorite Xcode6 feature is the new size classes concept, and with it, we've solved "how can I quickly write apps for so many different screen sizes and directions." They also let you create a generic app in a storyboard. By federating with the new Adaptive screen controller, it is easier to rely on interface builder than against it, simplifying the layout of your application.

Each view controller in the app has a special collection object, and this particular collection object has two size classes, a horizontal size class with a vertical size class. And each class has 3 possible values: Compact,regular, or any. These values can be changed based on the device and direction. Your app will layout its interface for each view controller based on the current size classes. Apple uses a grid to let the user choose which setting to work on, so here's a grid containing the device and the direction,

The corresponding size class combination is highlighted:

An interesting thing: the ipad is regular x regular. As Apple described in the "What's New in IOS8 Guide":

With the amount of screen space available, the IPad have a regular size class in the vertical and horizontal directions in Both portrait and landscape orientations. "

-dr. dre* "

All right, here's the book material, then open Xcode6 let's try it. According to Apple's NDA and XCODE6, and the following are all from XCODE5, so these pictures are just some guide.

Create a new generic project. If you want to be early in a XCODE6 project that has already been created, you need to activate the size classes option. You can find it in the Properties panel of Interface Builder by checking the AutoLayout option below.

First, let's look at the grid of size class in Xcode. This is an area where you can switch between different layout arrangements. When you look at the storyboard, see the bottom of the view, and click on the label ' Wany HAny '. You'll see some grid-like images.

(Translator: I don't have this grid in the original.) )

By default, we start with a basic setting, which is the any width and any height. Many things will be placed and changed here, including the default layouts for all directions of the iphone and ipad. Apple recommends setting most of the settings in this interface. This is particularly simple because of the reduced workload. Let's lay out a super wide button in the middle of the screen. Give it a green background so that we can see its true size and give it a restraint to center on it.

and gives it an exaggerated fixed width of 600.

OK, now that the ipad and iphone simulator are running, you will see the center, but for the iphone two directions are too wide, (here you set the width of the button in the page but not immediately update is because you do add constraints when you do not update the graphics, resulting in the situation , storyboard inside no update, and in the emulator run time update, left outline column inside also has warning, you can directly click on the yellow triangle inside the warning to update the screen is actually updata frame)

Let's use size classes to fix it. Go back to the grid of the first picture. Select the Portrait (portrait) setting of the iphone, which is the compact width + normal height. The red rectangle in the grid.

You will notice that the bar at the bottom is changed to blue after you select it in the grid. That is warning you: "Hey, you are not in a basic setting, and some changes will be displayed only when you are running." So this bar is now blue! "Some of the changes I've made are because there are four things you can change size classes:1 constraint constants, 2 fonts, 3 constraints on/off, and 4 child views on/off.

The first two are self-evident, but let me show you how to make the latter two work. In the current size class (compact width and regular height), let's try to close a constraint. In the outline section of the document, click the Centre X Calibration constraint set on our button:

Now look at our property check bar, at the bottom we can see a tagged word "installed", and an extra plus button on the left side. Click on the additional plus sign and click on ' Compact width| Regular Height ' (current is).

Now you will see 2 markers, which uncheck the one you just added (WC hR)

Now our constraints are no longer placed and do anything to configure size classes. As you can see, Xcode is accusing us of being too messy (the outline on the left has an error indicating that you are missing the constraint-translator), and if you run the app on the iphone emulator at this time, the button is not centered in the x direction. But it's still centered on the ipad, because the constraints are still placed in the basic settings. This constraint will always be configured unless we uncheck it. You can even rotate your iphone simulator and find that the button will magically go back to the center, because the iphone's landscape is different size class configuration, OK, let's put the tick back, let the button back to the center.

Now let's change the constraint we set on the button width, select the button, and come to the property check bar of size, drop down to the bottom and we can see all the constraints. Click Width originally to be 600 using edit set to 100:

Run on the iphone emulator and you will see that the button has the correct width. When I ran the ipad simulator, it showed a width of 600 because we didn't change the width of the basic settings. However, the horizontal landscape on the iphone still looks bad because the iphone's landscape settings come from the basic any of the settings. Let's fix it. Inside the grid we select compact width and compact Height. The blue grid of the first picture.

Now we change the width constraint in this setting, just as we changed for the compact X regular. Give a width of 400. Run the iphone simulator and rotate to landscape, the button has a width of 400 and looks great. reached our expectations. One thing that's good is that you can see a list of all the constraints that are different settings. Just select the constraints you want to see in the document outline, and then come to the property check bar, and they are neatly arranged under the initial constants. It marks each one based on the settings it applies to.

Even though we decided we wanted to disappear only in the iphone landscape landscape mode, using size classes we just reverse-place the views as if we were to place a constraint backwards. Select our UIButton and scroll to the bottom of the property inspector. Add a new placement option to our current settings by clicking the Plus button, then uncheck it.

As you can see, the view disappears immediately, because we put it back in the setup and we see it right away. Running the app, you can see it disappearing on the portrait portrait iphone, but it's back when you rotate to the horizontal landscape. Of course it has been placed on the ipad because the ipad is still using the basic settings.

The final thing is that we want to show how to prompt these settings in code for changes. Size classes are generally replaced by uiinterfaceorientation or UIUSERINTERFACEIDIOM. So it's important to understand how to work in code. The first thing you need to do is set up your view controller to conform to the Uitraitenvironment protocol:

123456 class ViewController: UIViewController, UITraitEnvironment {   override func viewDidLoad(){ //code }}

This protocol requires a required method to tell you that the view controller trait collection has changed:

123456 < Code class= "JS Plain" >override func traitcollectiondidchange (previoustraitcollection:  uitraitcollection!)  { &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; if  previoustraitcollection? { &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;           println (previoustraitcollection) &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; } &NBSP;&NBSP;&NBSP;&NBSP; }

A uitraitcollection provides a detail that includes the properties of the View controller:

Both of these descriptions are the horizontal or vertical size class that we just changed, here is the enumeration:

12345 enum UIUserInterfaceSizeClass : Int {    caseUnspecified    case Compact    caseRegular}

 

Size Classes with Xcode 6: Prepare a storyboard for all sizes

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.