7.7 · Trait collections, size classes, safe area, keyboard and appearance
One app runs on many screens, orientations, text sizes and colour schemes. Traits, safe areas and the appearance APIs are how a single layout adapts instead of branching on device models.
Mental model. Do not ask which device this is. Ask the environment what it is right now (traits) and which parts of the screen are usable (safe area), then let the layout follow.
Adapting to the environment
Traits and size classes, the safe area and the keyboard.
- What is a trait collection, and how do size classes and trait changes drive adaptive layout?
A UITraitCollection describes the environment a view or controller is in: horizontalSizeClass and verticalSizeClass (compact or regular), userInterfaceStyle… - What is the safe area, and how do you keep content clear of the notch, home indicator and keyboard?
The safe area is the part of a view not covered by system chrome: the status bar, Dynamic Island or notch, home indicator, navigation bar and tab bar, plus any…
Look and feel
Dark mode, dynamic colours and the appearance proxy.
- How do dynamic colours, dark mode and the UIAppearance proxy work?
Dynamic colours make dark mode nearly free. A colour such as UIColor.label, or an asset-catalogue colour with light and dark variants, or UIColor { traits in ……
More in UIKit
- 7.1 · Hit testing, the responder chain and gesture recognisers
- 7.2 · UIView and CALayer, frame, bounds and center
- 7.3 · View controller lifecycle, containment, presentation and transitions
- 7.4 · Navigation, tab, split and page controllers
- 7.5 · Compositional layout, diffable data sources, cell reuse and prefetching
- 7.6 · UITableView, still asked and still everywhere
- 7.8 · Keyboard shortcuts, pointer, Apple Pencil and multitasking
- 7.9 · Hosting SwiftUI in UIKit and UIKit in SwiftUI
- 7.10 · Initialisers, stack views, scroll views and modern buttons
- 7.11 · Layout passes, retain cycles and menus
- 7.12 · Refreshing diffable cells, outlines and carousel layouts
- 7.13 · View-tree exercises and long navigation stacks