Swift Size Class that can be understood in 3 seconds
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
- Horizontal Size Class
- Vertical Size Class
Theses are used for being adaptive when screen orientation changes.
Size Classe’s Traits
Each Size Class has two traits.
- compact
- 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!
- 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
(2) UIKit Example
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.