我有一个应用程序,我在它的目标设置中将方向设置为纵向:
在一个特定的 View Controller 中,我想覆盖此设置,以便自动布局将在设备旋转时更新 View 。我试过这些方法都没有成功:
override func shouldAutorotate() -> Bool {
return true
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.AllButUpsideDown
}
最佳答案
如果我已经阅读了接受的答案,我就不必自己写了。我的版本更长,但只需要应用程序委托(delegate)的 application(_:supportedInterfaceOrientationsFor:)
方法。它可能适用于您无法更改或不想更改目标 View Controller 的情况。例如,第三方 View Controller 。
我的灵感来自 Apple 的官方文档:supportedInterfaceOrientations
我的应用程序在 iPhone 上以纵向运行,在 iPad 上以所有方向运行。我只希望一个 View Controller (一个 JTSImageViewController
呈现一个更大 View 的图像)能够旋转。
信息.plist
Supported interface orientations = Portrait
Supported interface orientations (iPad) = Portrait, PortraitUpsideDown, LandscapeLeft, LandscapeRight
如果应用委托(delegate)实现了application(_:supportedInterfaceOrientationsFor:)
,Info.plist 可能会被忽略。但是,我没有验证这一点。
swift 4
func application(_ application: UIApplication,
supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
// Early return for iPad
if UIDevice.current.userInterfaceIdiom == .pad {
return [.all]
}
// Search for the visible view controller
var vc = window?.rootViewController
// Dig through tab bar and navigation, regardless their order
while (vc is UITabBarController) || (vc is UINavigationController) {
if let c = vc as? UINavigationController {
vc = c.topViewController
} else if let c = vc as? UITabBarController {
vc = c.selectedViewController
}
}
// Look for model view controller
while (vc?.presentedViewController) != nil {
vc = vc!.presentedViewController
}
print("vc = " + (vc != nil ? String(describing: type(of: vc!)) : "nil"))
// Final check if it's our target class. Also make sure it isn't exiting.
// Otherwise, system will mistakenly rotate the presentingViewController.
if (vc is JTSImageViewController) && !(vc!.isBeingDismissed) {
return [.allButUpsideDown]
}
return [.portrait]
}
关于ios - 覆盖应用程序方向设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36358032/