UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

What are size classes?

Swift version: 5.6

Paul Hudson    @twostraws   

Size Classes are the iOS method of creating adaptable layouts that look great on all sizes and orientations of iPhone and iPad. For example, you might want to say that your UI looks mostly the same in portrait and landscape, but on landscape some extra information is visible. You could do this in code by checking for a change in the size of your view controller and trying to figure out what it means, but that's a huge waste of time – particularly now that iPad has multiple different sizes thanks to multitasking in iOS 9.

With Size Classes, you don't think about orientation or even device size. You care about whether you are running in a compact size or regular size, and iOS takes care of mapping that to various device sizes and orientations. iOS will also tell you when your size class changes so you can update your UI.

For example, an iPad app running full screen in portrait has regular horizontal and vertical size classes. In landscape, it also has regular horizontal and vertical size classes. If your app is used in iOS 9 multitasking, then its size class can be one of the following:

  • If the apps are running with an even split in landscape, both have compact horizontal and regular vertical size classes.
  • If the apps are running with an uneven split in landscape, the primary app has a regular horizontal class and the second has a compact horizontal size class. Both apps have regular vertical classes.
  • If the apps are running with an uneven split in portrait, both apps have compact horizontal size classes and regular vertical size classes.

Size Classes can be implemented in code if you want, but it's much easier to use Interface Builder. The key is to change only the bits you have to – try to share as much of your user interface as possible!

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Available from iOS 8.0 – see Hacking with Swift tutorial 31

Similar solutions…

About the Swift Knowledge Base

This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.

Buy Pro Swift Buy Pro SwiftUI Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let us know!

Average rating: 3.8/5

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.