Ios 如何始终以纵向方式加载应用程序?
应用程序在启动时必须是纵向的,但在我可以旋转它之后Ios 如何始终以纵向方式加载应用程序?,ios,swift,orientation,Ios,Swift,Orientation,应用程序在启动时必须是纵向的,但在我可以旋转它之后 如何做到这一点,可能是通过编程实现的?您可以将其添加到希望在纵向模式下使用的每个控制器中: override var supportedInterfaceOrientations: UIInterfaceOrientationMask { return UIInterfaceOrientationMask.portrait //return the value as per the required orientation } 或者您
如何做到这一点,可能是通过编程实现的?您可以将其添加到希望在纵向模式下使用的每个控制器中:
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.portrait //return the value as per the required orientation
}
或者您可以在设置中设置它(正如@Sid Mhatre已经提到的):您可以将它添加到您希望在纵向模式下使用的每个控制器:
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.portrait //return the value as per the required orientation
}
或者您可以在设置中设置它(正如@Sid Mhatre已经提到的):您可以在appDelegate中为方向创建一个扩展,如下所示:
protocol MyAppDelegate: class {
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask
}
extension AppDelegate: MyAppDelegate {
func application(_ application: UIApplication,
supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return shouldRotate ? .allButUpsideDown : .portrait
}
}
在要允许旋转的ViewController中,应在viewDidLoad方法中实现这一点:
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.shouldRotate = true
并在查看时再次删除它将消失:
override func viewWillDisappear(_ animated: Bool) {
UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.shouldRotate = false
}
您可以在appDelegate中创建方向扩展,如下所示:
protocol MyAppDelegate: class {
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask
}
extension AppDelegate: MyAppDelegate {
func application(_ application: UIApplication,
supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return shouldRotate ? .allButUpsideDown : .portrait
}
}
在要允许旋转的ViewController中,应在viewDidLoad方法中实现这一点:
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.shouldRotate = true
并在查看时再次删除它将消失:
override func viewWillDisappear(_ animated: Bool) {
UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.shouldRotate = false
}
问题是什么?请查看此Ans:@SidMhatre OP声明应用程序必须支持横向定位。他只想在启动时使用它,“加载应用程序”对您意味着什么?通常,它是一些飞溅的图像,挂起,直到应用程序可以正常运行。你在做其他事情吗?如果是,请在问题中指定它们。@lolbas阅读建议的问题和答案链接。仅适用于纵向模式问题是什么?请查看此Ans:@SidMhatre OP声明应用程序必须支持横向。他只想在启动时使用它,“加载应用程序”对您意味着什么?通常,它是一些飞溅的图像,挂起,直到应用程序可以正常运行。你在做其他事情吗?如果是,请在问题中指定它们。@lolbas阅读建议的问题和答案链接。它仅用于纵向模式