Why this happens
By default, orientation support is app-wide. The values you set in Target > General > Deployment Info or in Info.plist under UISupportedInterfaceOrientations apply to every screen. iOS asks your app delegate’s application(_:supportedInterfaceOrientationsFor:) what is allowed, and that answer wins.
Each UIViewController also has supportedInterfaceOrientations and shouldAutorotate, but containers like UINavigationController and UITabBarController do not forward those calls to their children by default. If your portrait-only screen is inside a navigation or tab controller, overriding those properties on the child alone does nothing. The system never asks the child.
That is why locking one screen requires a global switch that you can flip when that screen appears and flip back when it disappears, instead of relying on the view controller hierarchy to propagate the preference.
How to fix it
The reliable pattern is to keep a single orientationLock in your AppDelegate and update it from the view controller that needs to be portrait-only. This works whether the view controller is pushed, shown in a tab, or presented modally.
1. Add a global lock to AppDelegate
In AppDelegate.swift, add a property to store the current mask and return it from the system delegate method. The default should be what the rest of your app supports.
import UIKit
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
// Default for the whole app. Use .all if you support all orientations elsewhere.
var orientationLock: UIInterfaceOrientationMask = .all
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return orientationLock
}
}
If you use UISceneDelegate (iOS 13+), keep this code in AppDelegate. The system still calls application(_:supportedInterfaceOrientationsFor:) on the app delegate for the window. Do not duplicate it in the scene delegate.
2. Create a small helper to update the lock
A helper keeps the view controller code clean and avoids repeating the cast to AppDelegate. Create it in a shared file.
import UIKit
struct AppUtility {
static func lockOrientation(_ orientation: UIInterfaceOrientationMask) {
if let delegate = UIApplication.shared.delegate as? AppDelegate {
delegate.orientationLock = orientation
}
}
// Call this when you want to lock and immediately rotate if needed
static func lockOrientation(_ orientation: UIInterfaceOrientationMask, andRotateTo rotateOrientation: UIInterfaceOrientation) {
lockOrientation(orientation)
UIDevice.current.setValue(rotateOrientation.rawValue, forKey: "orientation")
UINavigationController.attemptRotationToDeviceOrientation()
}
}
UIDevice.current.setValue(_:forKey: "orientation") is a KVC trick to force the device orientation value, and attemptRotationToDeviceOrientation() tells the system to re-evaluate the interface orientation. The KVC key is not a public API but is widely used for this purpose. If you prefer to avoid it, use the lockOrientation(_:) version without rotation and let the system rotate naturally when the user turns the device. The lock will still prevent landscape.
3. Lock in viewWillAppear and reset in viewWillDisappear
In the view controller that must stay in portrait, set the lock when it will appear and reset it when it will disappear. Do this in viewWillAppear / viewWillDisappear, not viewDidLoad, so it runs every time the screen is shown and correctly resets when you go back.
import UIKit
class SignInViewController: UIViewController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Lock to portrait only
AppUtility.lockOrientation(.portrait)
// Or lock and force rotation to portrait immediately:
// AppUtility.lockOrientation(.portrait, andRotateTo: .portrait)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
// Restore for the rest of the app
AppUtility.lockOrientation(.all)
}
}
If the view controller can be dismissed interactively or you push another controller on top without viewWillDisappear being called on the locked controller, also reset in viewDidDisappear or in the next controller’s viewWillAppear to guarantee the lock is cleared.
For a modal presentation, present with the default full-screen style or ensure the presenting controller also resets. A .pageSheet or .formSheet on iPad may not trigger rotation the same way as full screen.
4. Fix iPad and multitasking behavior
On iPad, supportedInterfaceOrientationsFor is not called if your app supports multitasking. To make the lock work on iPad or a Universal app, enable Requires Full Screen:
- Select your target in Xcode.
- Go to General > Deployment Info.
- Check
Requires full screen.
Alternatively, set UIRequiresFullScreen to YES in Info.plist. Without this, iPad multitasking (Split View, Slide Over) overrides your orientation mask and the delegate method is ignored.
Also verify your Info.plist actually lists all orientations you want for the rest of the app under UISupportedInterfaceOrientations and UISupportedInterfaceOrientations~ipad. If portrait is the only entry there, .all will have no effect.
5. When you don’t want the forced rotation
If you only call AppUtility.lockOrientation(.portrait), the interface will stay in its current orientation until the user rotates the device or the system asks for a rotation. The app will not rotate to landscape, but if the user was already in landscape when the screen appeared, it will stay landscape until they rotate to portrait.
Use lockOrientation(.portrait, andRotateTo: .portrait) when you need the screen to snap to portrait immediately, for example for a sign-in or camera capture screen that must be portrait from the moment it appears.
How to prevent it
- Always reset the lock. Every
viewWillAppearthat sets.portraitneeds a matchingviewWillDisappearthat sets.all(or whatever your app default is). Forgetting the reset leaves the whole app stuck in portrait. - Put the logic in
viewWillAppear/viewWillDisappear, notviewDidLoadorviewDidAppear.viewDidLoadruns once, andviewDidAppearruns too late and causes a visible rotation jump. - Test the navigation flow: push, pop, swipe-back, and modal dismiss. Make sure the previous controller regains its expected orientations.
- Check iPad with Requires Full Screen off and on. If you need multitasking, you cannot force orientation and should design the portrait-only screen to work in any size class instead.
- Keep the
Info.plistorientation arrays in sync with yourorientationLockdefaults. If you add.allin code but only list portrait inInfo.plist, landscape will never be allowed.
FAQ
Does overriding supportedInterfaceOrientations on the view controller work alone?
Only if that view controller is the root or its container forwards the call. UINavigationController and UITabBarController implement their own supportedInterfaceOrientations and do not ask their children. You would need to subclass the container to forward the value, which is more fragile than the AppDelegate lock pattern when you have tabs and navigation stacks.
Why does my lock work on iPhone but not on iPad?
On iPad, multitasking disables the app delegate orientation callback. If Requires full screen is not checked, application(_:supportedInterfaceOrientationsFor:) is not called and your orientationLock is ignored. Enable Requires Full Screen or accept that iPad will not lock when multitasking is supported.
Is UIDevice.current.setValue(forKey: “orientation”) safe for the App Store?
It uses KVC to set a private property. Apple has not rejected apps solely for this line when used to force orientation, and it is the standard workaround shown in many production apps. If you want to avoid any private API usage, skip that line and only set orientationLock. The screen will still be locked to portrait; it just will not animate to portrait automatically if the device is currently in landscape.
Parts of this guide are adapted from content licensed under CC BY-SA 4.0; this article is shared under the same license.
