iOS How to run a function after Device has Rotated Swift
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
On iOS, the safest way to run code after a rotation is usually through the view controller transition lifecycle, not by guessing based on raw device-orientation notifications. If your goal is to update layout, restart an animation, or recalculate geometry after the interface changes size, use the APIs that are tied to the actual rotation transition.
Prefer viewWillTransition(to:with:)
For most UIKit apps, viewWillTransition(to:with:) is the right place to respond to a rotation. It is called when the interface is about to change size, and the transition coordinator lets you run code during or after the rotation animation.
If you truly need the function after the rotation completes, put it in the coordinator's completion block. That timing is better than trying to guess whether the screen has already settled.
Why Orientation Notifications Are Not the Best Default
You can observe UIDevice.orientationDidChangeNotification, but that notification tracks the device's physical orientation, not necessarily the interface rotation you care about.
For example, the device can report .faceUp, .faceDown, or an orientation change that does not lead to a visible interface rotation. That makes notification-based code noisy for layout work.
A notification approach is still possible:
That is acceptable when you genuinely care about raw device orientation, but it is usually the wrong tool for post-rotation UI work.
Running Code After Auto Layout Has Settled
Sometimes what you actually need is not "after rotation" but "after views have their final frames." In that case, viewDidLayoutSubviews() can be a better place to act, especially if your logic depends on final view sizes.
This pattern is useful when rotation changes constraints and you need the final layout before doing expensive recalculation.
Trait Changes and Size Classes
On modern iOS, rotation is not the only reason the interface may change. Split view, slide over, and iPad multitasking can also change available size. That is why size-based APIs are often better than raw orientation checks.
If the behavior depends on compact versus regular width, size classes may matter more than portrait versus landscape. Thinking in terms of available space makes the code more robust across devices.
Common Pitfalls
One common mistake is relying on UIDevice.current.orientation for layout decisions. The physical device orientation is not always the same as the current interface state.
Another problem is doing work too early. If you read frames before the rotation transition or Auto Layout pass finishes, you may calculate with stale sizes.
Developers also sometimes forget that not every size change is a traditional rotation. On iPad, the app can resize without the device turning at all, so code that only thinks in terms of portrait and landscape becomes brittle.
Finally, if you use notifications, remember to remove observers when appropriate. Otherwise the controller can keep receiving rotation events after it should be gone.
Summary
- Use
viewWillTransition(to:with:)for most rotation-related UIKit work. - Put post-rotation logic in the transition coordinator completion block.
- Use
viewDidLayoutSubviews()when you need final view sizes after layout. - Avoid using raw orientation notifications for layout unless you truly need device orientation.
- Prefer size-based thinking over portrait-versus-landscape assumptions.
Related reading
- iOS How to run a function after Device has Rotated Swift
- iOS how to set app icon and launch images
- iOS How to store username/password within an app?
- iOS Image Orientation has Strange Behavior
- iOS iPhone, iPad, iPodTouch view real-time console log terminal
- IOS Issues with sendAsynchronousRequest
- iOS JavaScript bridge
- iOS Keeping old launch screen and app icon after update
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.