仅在纵向方向启动应用程序



我正试图在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
}
}

试试看,让我知道它是否有效。

最新更新