我有一个带有多个 View Controller 的应用程序,其中大部分应该能够有任何方向,但一个必须是纵向的。但是,应用程序忽略了我从 supportedInterfaceOrientations() 返回的值
我的代码:
UINavigationController subclass:
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
if let topViewController = self.topViewController {
return topViewController.supportedInterfaceOrientations()
}
return [.Landscape, .Portrait]
}
override func shouldAutorotate() -> Bool {
if let topViewController = self.topViewController {
return topViewController.shouldAutorotate()
}
return true
}
UIViewController subclass
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return .Portrait
}
override func shouldAutorotate() -> Bool {
return true
}
当我在横向兼容的 View Controller 中转到本应为纵向的 View Controller 时,没有任何反应 - 应用程序不会自动旋转到纵向。
我也从 info.plist 中删除了支持的界面方向。有什么想法吗?
最佳答案
你可以在 AppDelegate.swift
中设置它们,在这个函数中
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow) -> Int {
return checkOrientationForVC(self.window?.rootViewController?)
}
在这个函数中你可以做类似的事情:
func checkOrientationForVC(viewController:UIViewController?)-> Int{
if(viewController == nil){
return Int(UIInterfaceOrientationMask.All.rawValue)//All means all orientation
}else if (viewController is SignInViewController){ //Your View controller here
return Int(UIInterfaceOrientationMask.Portrait.rawValue)
}else{
return checkOrientationForVC(viewController!.presentedViewController?)
}
}
还在“方向”的“模拟指标”下的 Storyboard 中设置方向。您还可以看到这个 Stackoverflow's post.
关于ios - 应用忽略 supportedInterfaceOrientations,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40677004/