Getting device orientation in Swift
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In iOS, "orientation" can mean two different things: the physical orientation of the device and the orientation of your interface. Most confusion comes from reading one when you actually need the other.
Device Orientation Versus Interface Orientation
UIDevice.current.orientation tells you how the hardware is positioned. UIWindowScene.interfaceOrientation tells you how the app interface is currently laid out.
These can differ. For example:
- the device can be face up while the interface stays portrait
- orientation lock can keep the UI fixed
- on iPad, window size and multitasking can matter more than raw device rotation
So the first step is deciding which signal you really need.
Reading Physical Device Orientation
If you need the hardware orientation, use UIDevice.current.orientation.
This is the right choice for camera behavior, motion-sensitive UI, or logic that truly depends on physical position.
Listening For Orientation Changes
If the value can change while the screen is visible, observe orientation change notifications.
This pattern is reliable when you need updates instead of a one-time check.
Reading Interface Orientation
If what you really care about is how the app is being displayed, use the scene's interface orientation.
This is often better than device orientation for layout decisions, because it reflects the UI rather than the raw hardware state.
Layout Is Often Better Than Orientation Checks
A lot of code that asks for orientation really wants responsive layout. In those cases, use size classes, view bounds, or Auto Layout constraints instead of branching on orientation.
For example, if the goal is "show two columns when the screen is wide," checking width is usually better than checking orientation.
This adapts better to iPad multitasking and resizable scenes.
A SwiftUI Pattern
In SwiftUI, you can still observe orientation notifications if needed:
Even there, many layout problems are better solved with geometry and adaptive views instead of raw orientation values.
Common Pitfalls
The most common mistake is using UIDevice.current.orientation for layout decisions when interface orientation or view size would be more accurate.
Another mistake is ignoring .unknown, .faceUp, and .faceDown. Those are valid device states and they can appear at app startup or while the device is flat.
Developers also forget to begin orientation notifications before expecting change events.
Finally, orientation checks can become brittle on iPad. If the real requirement is adaptive layout, rely on view size or size classes instead of assuming portrait versus landscape is the full story.
Summary
- Use
UIDevice.current.orientationfor physical device position. - Use
UIWindowScene.interfaceOrientationfor the app's interface orientation. - Observe
UIDevice.orientationDidChangeNotificationwhen you need live updates. - Prefer layout-driven logic over orientation checks when the problem is really screen size.
- Handle
.unknown,.faceUp, and.faceDownexplicitly.

