今天,做一个项目,本意是首页是要屏幕方向是固定的portrait,rootViewController是navigationController,不论开启App之前是横屏或竖屏,进到App就变成portrait.
要在UINavigationController的子类里加
override var shouldAutorotate: Bool{
return true
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .portrait
}
override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
return .portrait
}
preferredInterfaceOrientationForPresentation: UIInterfaceOrientation这个方法在启动时一直没有调用,根据字面的猜想它只在一个viewController presentation到下一具viewController时,下一个viewController会调用。
上面这样设置会对navigationController里所有viewController都有效,如果要对每个viewController都有自定义的设置,请看下面的文章
iOS 横竖屏切换(控制项目的每一个ViewController)
最好在func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool里加
UIApplication.shared.statusBarOrientation = .portrait