Swift
iOS Development
ViewController
Landscape Mode
Code Implementation

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.

Browse interview questions

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.

swift
1import UIKit
2
3final class PlayerViewController: UIViewController {
4    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
5        return .landscape
6    }
7
8    override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
9        return .landscapeRight
10    }
11}

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.

swift
1import UIKit
2
3final class PlayerViewController: UIViewController {
4    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
5        return .landscape
6    }
7
8    override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
9        return .landscapeRight
10    }
11
12    override func viewDidAppear(_ animated: Bool) {
13        super.viewDidAppear(animated)
14        setNeedsUpdateOfSupportedInterfaceOrientations()
15
16        if #available(iOS 16.0, *), let scene = view.window?.windowScene {
17            let preferences = UIWindowScene.GeometryPreferences.iOS(interfaceOrientations: .landscape)
18            scene.requestGeometryUpdate(preferences) { error in
19                print("Orientation update failed: \(error)")
20            }
21        }
22    }
23}

This is the current direction of the public API for requesting orientation updates.

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.

swift
1import UIKit
2
3final class RotationAwareNavigationController: UINavigationController {
4    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
5        return topViewController?.supportedInterfaceOrientations ?? .all
6    }
7
8    override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
9        return topViewController?.preferredInterfaceOrientationForPresentation ?? .portrait
10    }
11}

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 supportedInterfaceOrientations and preferredInterfaceOrientationForPresentation in 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
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.