Skip to main content

An introduction to Size Classes for Xcode 8

Introduction to Size Classes for Xcode



In iOS 8, Apple introduced size classes, a way to describe any device in any orientation. Size classes rely heavily on auto layout. Until iOS 8, you could escape auto layout. IN iOS8, Apple changed several UIKit classes to depend on size classes. Modal views, popovers, split views, and image assets directly use size classes to determine how to display an image. Identical code to present a popover on an iPad  causes a iPhone to present a modal view.
Different Size Classes
There are two sizes for size classes: compact, and regular. Sometime you’ll hear about any. Any is the generic size that works with anything. The default Xcode layout, is width:any height:any. This layout is for all cases. The Horizontal and vertical dimensions are called traits, and can be accessed in code from an instance of UITraitCollection. The compact size describes most iPhone sizes in landscape and portrait. The trait of most importance is the width trait. The width is compact on all phones but the iPhone Plus models. There is one exception: the width in landscape is regular for an iPhone 6 Plus, which can cause some confusion. iPhone 6 Plus acts like a iPhone in portrait but an iPad in landscape. For both width and height, the full iPad and the 2/3 iPad  for multitasking is the regular size. The 1/3 and 1/2 iPad multitasking modes are compact in width and regular in height.
There’s one more variation to compact and regular. You can specify both compact and regular with Any. When you add Any to Compact and Regular there are nine size classes the developer can use.
size classes 9_1_1
Prior to Xcode 8, developers would have to know what all these sizes are and which device each belonged. That changed in Xcode 8 with a new user interface for Interface Builder. Xcode displays as selection of devices, the developer selects the devices, then Xcode previews the layout on that device.

Viewing Size Classes

The  any class makes working with size classes a bit more generic, saving work.  Our designs start in the width:any height:any size class, covering all cases. If we need a special design, then we use that specific size class. Prior to Xcode 8,  any needed to be explicit, and in some places you’ll find explicit uses of any still. Often Xcode 8 hides Any from you, making it implicit instead of explicit.
Open a new single view  project in XCode.  Go to the storyboard.  At the bottom left storyboard you’ll find this:
2016-09-02_06-29-00
the iPhone 6s is the default device for Interface Builder. Click this text. A new toolbar appears below, with the iPhone6s in portrait selected.
2016-09-02_06-31-53
 Select the iPad Pro 12.9″ on the left. The toolbar changes to include all size classes for an iPad Pro.
2016-09-02_06-36-40
You’ll notice the screen looks blank. The iPad is too big for the screen. In the center of the toolbar click the 100%. In the menu that appears, click the 50%.
2016-09-02_06-40-09
To make the device easier to see, I changed the background color attribute of the view to light gray.
2016-09-02_06-43-44
Select  the iPhone 4s and under Orientation set the orientation to Landscape. We get a preview of a iPhone4s in landscape. Zoom in to 100% to see it better.
2016-09-02_06-47-16
All of this is in the Any size.  To change the size for devices, you can  click the Vary for Traits button.

A Little Auto Layout

So far you’ve previewed a blank storyboard.  Click the icon for a portrait phone. Add two buttons labeled Button 1  on the top of the scene and Button 2 towards the bottom. In the attributes inspector, Set the Text Color to White and the background of the button to Black.
2016-09-02_06-56-34


You’ll need a little auto layout to take advantage of size classes. If you’ve never used auto layout, we’ll keep it simple. Select Button 1.Find the pin button pinMenuButtonin the Auto Layout menu on the lower right side of Interface builder. Click pinMenuButtonto get a popup.
2016-09-02_07-01-19
In the highlighted box pin to the view above by typing 10 and hitting tab on your keyboard. The I-Beam below turns black, and the cursor moved to the left pin. Type 10 and tab again to pin the button to the right margin 10 points. Type 10 and tab one more time to pin the right side to the right margin by 10 points. Be sure to type tab to make sure the I-beam shows.  Toward the bottom of the popup, You’ll find the Update Frames button. Change the value from None to Items of new constraints. Your popup should look like this:
2016-09-02_07-07-01
Click Add 3 constraints and the constraints will appear on the storyboard, stretching the button to the correct size and position.
2016-09-02_07-11-20
Select Button 2 on the bottom. You’ll pin this to the bottom. Click pinMenuButtonto get a popup.  Tab past the first box without entering anything. try 10 then tab for the left pin, 10 and tab for the right pin, and 10 and tabfor the bottom pin. Again set Update frames to Items of new Constraints. The popup should look like this.
2016-09-02_07-16-10
Add the three constraints. Your storyboard should look like this:
2016-09-02_07-17-38
Click the portrait orientation icon portrait icon 4sand the view resizes the buttons:
2016-09-02_07-20-16
Click the iPad Pro 9.7″ icon. Close the navigation and attributes inspector panels to give yourself room.  Change the scale to 75%. You’ll see this:
2016-09-02_07-25-30


Varying a Trait

The horizontal and vertical dimensions we refer to as traits. You can change one or both traits of the displayed device. The current traits are found after the name of the device in the view as button.  The current view show us a width of regular(wR) and height of regular(hR). The  default  iPhone6s in portrait is (wC hR) for compact width, Regular height.
2016-09-02_07-30-41a
Select an iPhone 6s Plus(wR hC) in landscape in the toolbar.  All phones in landscape are compact width and compact height  except  iPhone Plus. The iPhone plus models  are  regular width not compact. On regular width devices,  usually users will hold with both hands on the sides, using their thumbs across the device for most button presses. On compact width devices, users typically will hold from the bottom for compact width devices, using the thumb up and down.  You’ll now change all devices with a regular width to place the buttons on the sides, which is the easier to use place for thumbs to contact them.
Click the Vary for Traits button on the right side of the size class toolbar.  A popup appears.
2016-09-02_07-43-42
Check the Width checkbox. The  toolbar changes color and displays all size classes affected.
2016-09-02_07-46-05
Click in the newly colored area to close the popup. You are now in the mode that changes only the devices with a regular class width.  Open the attributes inspector, and change to the ruler for the size inspector.
2016-09-02_07-48-38
Select the Button 2.  Scroll down the size inspector until you find this constraint.
2016-09-02_07-50-34

 Select it and press delete on your keyboard. Select Button 1. Find this constraint  and delete it:
2016-09-02_07-53-15
The storyboard looks like this:
2016-09-02_07-54-54
Select Button 1.  Click on the pin constraint button pinMenuButton. Select the bottom constraint and type 10 , then hit tab. Select Items of new constraints. The popup should look like this:
2016-09-02_07-58-38


Add the constraint. Select the Button 2. Type 10 , then hit tab for the top constraint. Select Items of new constraints like this:
2016-09-02_07-59-37 
Add the new constraint. The layout now looks like this:
2016-09-02_08-01-45
Press Done varying.  The highlight color disappears. Change to Portrait 2016-09-02_09-47-12and the buttons are on the top and bottom.

2016-09-02_08-48-58
Go to an iPad Pro 9.7″ in portrait.  At 75% scale it looks like this:
2016-09-02_09-48-53
Under Adaptations in the size class toolbar, click the middle button. This is a multitasking view, which is compact width. The buttons are on the top and bottom again.
2016-09-02_08-53-24

Class Size Variations on Attributes

You can also change attributes based on the size classes. Click the gray view.  Go to the attributes inspector and click the + next to the background attribute.
2016-09-02_08-55-17
A popup appears:
2016-09-02_09-05-07
The current device’s traits appear in the box. Change Width to Regular and  Height to Any to make this an attribute of all regular width devices.
2016-09-02_09-07-03
Click Add Variation. A new background attribute for regular width appears under the default one.
2016-09-02_09-08-03
Change the wR background color to another color. I used Orange(#FF8000). Nothing 
changes on the storyboard. You are still on a compact width. Click the  2016-09-02_09-12-10 icon in Adaptation to go back to regular size. The background is Orange.
2016-09-02_09-13-32 
You can browse though the other devices to see which ones have orange backgrounds and which have gray ones. As you can see, Size Classes in Xcode 8 are quite powerful ways of laying out your project easily, while customising the layout for different devices.

Comments

Popular Posts

How I Reduced the Size of My React Native App by 85%

How and Why You Should Do It I borrowed 25$ from my friend to start a Play Store Developer account to put up my first app. I had already created the app, created the assets and published it in the store. Nobody wants to download a todo list app that costs 25mb of bandwidth and another 25 MB of storage space. So today I am going to share with you how I reduced the size of Tet from 25 MB to around 3.5 MB. Size Matters Like any beginner, I wrote my app using Expo, the awesome React Native platform that makes creating native apps a breeze. There is no native setup, you write javascript and Expo builds the binaries for you. I love everything about Expo except the size of the binaries. Each binary weighs around 25 MB regardless of your app. So the first thing I did was to migrate my existing Expo app to React Native. Migrating to React Native react-native init  a new project with the same name Copy the  source  files over from Expo project Install all de...

How to recover data of your Android KeyStore?

These methods can save you by recovering Key Alias and Key Password and KeyStore Password. This dialog becomes trouble to you? You should always keep the keystore file safe as you will not be able to update your previously uploaded APKs on PlayStore. It always need same keystore file for every version releases. But it’s even worse when you have KeyStore file and you forget any credentials shown in above box. But Good thing is you can recover them with certain tricks [Yes, there are always ways]. So let’s get straight to those ways. 1. Check your log files → For  windows  users, Go to windows file explorer C://Users/your PC name/.AndroidStudio1.4 ( your android studio version )\system\log\idea.log.1 ( or any old log number ) Open your log file in Notepad++ or Any text editor, and search for: android.injected.signing and if you are lucky enough then you will start seeing these. Pandroid.injected.signing.store.file = This is  file path where t...

React Native - Text Input

In this chapter, we will show you how to work with  TextInput  elements in React Native. The Home component will import and render inputs. App.js import React from 'react' ; import Inputs from './inputs.js' const App = () => { return ( < Inputs /> ) } export default App Inputs We will define the initial state. After defining the initial state, we will create the  handleEmail  and the  handlePassword  functions. These functions are used for updating state. The  login()  function will just alert the current value of the state. We will also add some other properties to text inputs to disable auto capitalisation, remove the bottom border on Android devices and set a placeholder. inputs.js import React , { Component } from 'react' import { View , Text , TouchableOpacity , TextInput , StyleSheet } from 'react-native' class Inputs extends Component { state = { ...