import UIKit final class AppDelegate: NSObject, UIApplicationDelegate { static var orientationLock: UIInterfaceOrientationMask = .portrait func application( _ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow? ) -> UIInterfaceOrientationMask { AppDelegate.orientationLock } static func lockOrientation(_ mask: UIInterfaceOrientationMask) { orientationLock = mask guard let scene = UIApplication.shared.connectedScenes .compactMap({ $0 as? UIWindowScene }) .first(where: { $0.activationState == .foregroundActive }) ?? UIApplication.shared.connectedScenes.compactMap({ $0 as? UIWindowScene }).first else { return } func update(_ controller: UIViewController?) { guard let controller else { return } controller.setNeedsUpdateOfSupportedInterfaceOrientations() update(controller.presentedViewController) controller.children.forEach { update($0) } } let requested: UIInterfaceOrientationMask if mask.contains(.landscapeRight) { requested = .landscapeRight } else if mask.contains(.landscapeLeft) { requested = .landscapeLeft } else { requested = mask } scene.windows.forEach { update($0.rootViewController) } scene.requestGeometryUpdate(.iOS(interfaceOrientations: requested)) { _ in } DispatchQueue.main.async { scene.windows.forEach { update($0.rootViewController) } scene.requestGeometryUpdate(.iOS(interfaceOrientations: requested)) { _ in } } } }