Force landscape mode in one ViewController using Swift
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Forcing landscape for one view controller on iOS is partly a controller-level decision and partly an app-level capability decision. The view controller must declare that it supports landscape, and the app must also allow landscape in its supported orientations. On modern iOS, programmatically requesting the rotation uses a different API than older examples, so outdated snippets can mislead you.
Start With Supported Orientations
At the view-controller level, override supportedInterfaceOrientations and preferredInterfaceOrientationForPresentation.
This tells UIKit that the controller wants landscape orientation.
However, this alone does not guarantee a visible rotation if the app or scene configuration does not permit landscape at all.
The App Must Also Allow Landscape
If the app's supported interface orientations exclude landscape entirely, no controller-level override can make landscape happen. Check the app's orientation settings in the project configuration or Info.plist and ensure landscape is allowed for the relevant device family.
A useful rule is:
- app configuration defines what is allowed globally
- the view controller narrows that set for its own screen
If the app is portrait-only at the project level, this controller cannot become landscape.
Request Rotation on Modern iOS
Apple's iOS 16 release notes introduced scene-based rotation requests through UIWindowScene.requestGeometryUpdate, and deprecated the older attemptRotationToDeviceOrientation pattern. A modern implementation can request the desired orientation when the controller appears.
This is the current direction of the public API for requesting orientation updates.
Navigation Controllers Can Override Your Decision
If the landscape controller sits inside a UINavigationController, the navigation controller can determine orientation behavior unless it defers to the top controller.
A common fix is to subclass the navigation controller and forward orientation decisions.
Without this, you may implement the landscape logic correctly in the child controller and still see no effect because the container controls orientation.
Presenting One Landscape Screen in a Mostly Portrait App
A common pattern is to keep the app generally portrait and allow one screen, such as a media player, to use landscape. This works best when the view controller is presented full screen or hosted in a rotation-aware container.
If the controller is embedded in a more complex hierarchy, confirm which container actually answers UIKit's orientation questions.
Common Pitfalls
The biggest mistake is trying to force landscape while the app configuration itself does not allow landscape orientations.
Another issue is copying older code that changes device orientation directly or relies on deprecated rotation triggers. Modern iOS expects supported orientations plus scene-based geometry updates.
Developers also often forget that a navigation controller or tab controller may be the real orientation authority.
Finally, do not confuse "supports landscape" with "will instantly rotate into landscape." The controller, container, and scene all have to agree.
Summary
- Override
supportedInterfaceOrientationsandpreferredInterfaceOrientationForPresentationin the target controller. - Make sure the app configuration allows landscape globally.
- On modern iOS, request rotation through the scene geometry update API.
- If the controller is inside a navigation controller, make sure the container forwards orientation decisions.
- Treat orientation as a controller, container, and scene configuration problem, not just a single override.
Related reading
- Format UILabel with bullet points?
- Formatting a UITextField for credit card input like xxxx xxxx xxxx xxxx
- Formatting a UITextField for credit card input like xxxx xxxx xxxx xxxx
- Fragment MyFragment not attached to Activity
- Fragment onResume onPause is not called on backstack
- Fragment over another fragment issue
- ''Framework not found'' in Xcode
- frequent issues arising in android view, Error parsing XML unbound prefix
.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.