Swift Size Class that can be understood in 3 seconds

KD Knowledge Diet
2 min readMar 20, 2022

--

Swift Size Classes

I was shocked to see a developer with more than 10 years of ios says that the concept of size class is obsolete in a production environment because it is ambiguous. But it’s a really easy concept. And it is often used in my company, is it because the people I work with are stupid?

What is Size Class?

What the heck is Size Classes? There are two types of Size Classes

  1. Horizontal Size Class
  2. Vertical Size Class

Theses are used for being adaptive when screen orientation changes.

Size Classe’s Traits

Screen Orientation

Each Size Class has two traits.

  1. compact
  2. regular

compact means narrow space, and regular means wider space. That’s all you have to understand.

Let’s repeat it.

  • compact means narrow space
  • regular means wider space

Make sure you understand it!

compact & regular
  • If your phone is in portrait mode, what would be the horizontal class’s trait?

===> Answer: compact

  • If your phone is in landscape mode, what would be the horizontal class’s trait?

===> Answer: regular

#Size Notes

  • Some iPhone devices have the compact size on both horizontal and vertical class because it’s all narrow!
  • Most iPad devices have the regular size on both horizontal and vertical class because it’s all wide!

Code Example

(1) SwiftUI Example

Size Classes in SwiftUI

(2) UIKit Example

Size Classes in UIKit

Quiz Time

Before starting a quiz, let’s repeat again!

  • compact means narrow space
  • regular means wider space

(1) Is it most likely to be landscape or portrait?

horizontalClass == .compact && verticalClass == .regular

===> Answer: It’s most likely to be IPhone Portrait Mode.

(2) Is it portrait or landscape?

horizontalClass == .regular && verticalClass == .compact

===> Answer: It’s most likely to be IPhone Landscape Mode.

(3) Is it portrait or landscape?

horizontalClass == .regular && verticalClass == .regular

===> Answer: It’s most likely to be IPad.

Conclusion

It’s that easy. No complex photo or verbose explanation needed. Just remember these.

  • .regular means wide space.
  • .compact means narrow space.

--

--

KD Knowledge Diet

Software Engineer, Mobile Developer living in Seoul. I hate people using difficult words. Why not using simple words? Keep It Simple Stupid!