Swift version: 5.2
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:
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!
SPONSORED Announcing NSSpain 2020: Remote Edition! An online, continuous conference for iOS developers. We’ll start on Thursday and finish on Friday, with talks, activities, and lots of fun for 36 hours, non-stop. Sound good? Join us!
Available from iOS 8.0 – see Hacking with Swift tutorial 31
This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.
Link copied to your pasteboard.