Dynamically adjusting layouts based on rotation means automatically changing the UI arrangement of an app when the device switches between:
👉 The goal is to ensure the interface always looks proper, readable, and usable in both orientations.
A layout that changes automatically based on screen size or orientation.
Occurs when the device changes orientation, triggering a UI update.
Device Rotates
↓
iOS detects orientation change
↓
View updates layout constraints
↓
UI rearranges automatically
👉 Example:
| Portrait | Landscape |
|---|---|
| Vertical stack | Horizontal stack |
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
if size.width > size.height {
print("Landscape Layout")
} else {
print("Portrait Layout")
}
}
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
if traitCollection.verticalSizeClass == .compact {
print("Landscape Mode Detected")
}
}
if UIDevice.current.orientation.isLandscape {
label.font = UIFont.systemFont(ofSize: 24)
button.frame.origin.y = 100
} else {
label.font = UIFont.systemFont(ofSize: 18)
button.frame.origin.y = 200
}
Draw:
Portrait Mode Landscape Mode
📱 📱
[ UI Stack ] [ UI UI UI ]
[ Vertical ] [ Horizontal ]
Portrait:
Landscape:
👉 Layout automatically adjusts based on orientation
Dynamic layout = UI changes automatically on rotation
iOS uses:
Rotation triggers UI updates
Methods:
viewWillTransitiontraitCollectionDidChangeBest practice: always design responsive UI
Open this section to load past papers