仅以纵向启动应用程序

Launching an app only in portrait orientation

我正在尝试在 Swift 上开发一款同时支持纵向和横向的应用程序,但我不希望用户能够以横向模式启动它。 那么有没有一种方法可以防止这种横向打开,同时将应用程序的设备方向参数保持在纵向、横向左侧和横向右侧,或者有没有一种方法可以在使用 UIInterfaceOrientationMask 启动应用程序后将设备方向参数从纵向更改为纵向?

非常感谢您的帮助!

限制在AppDelegate的方法中-

var supportedOrientation: UIInterfaceOrientationMask = .portrait

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    return supportedOrientation  
}

覆盖所有视图控制器中的跟随 -

override func viewDidAppear(_ animated: Bool) {
    if let delegate = UIApplication.shared.delegate as? AppDelegate {
        var orientation: UIInterfaceOrientationMask = .all
        delegate.supportedOrientation = orientation
    }
}

尝试一下,如果有效请告诉我。