如果我已經閱讀接受的答案,我不會寫我自己的。我的版本更長,但只需要應用程序代理的方法application(_:supportedInterfaceOrientationsFor:)
。它可能適用於您無法更改或不希望更改目標視圖控制器的情況。例如,第三方視圖控制器。
我是從蘋果的官方文檔的啓發:supportedInterfaceOrientations
我的應用程序運行作爲肖像在iPhone和iPad上的所有方向。我只想要一個視圖控制器(一個JTSImageViewController
呈現一個大圖的圖像)能夠旋轉。
的Info.plist
Supported interface orientations = Portrait
Supported interface orientations (iPad) = Portrait, PortraitUpsideDown, LandscapeLeft, LandscapeRight
如果應用程序委託實施application(_:supportedInterfaceOrientationsFor:)
,Info.plist中可能會被忽略。但是,我沒有證實這一點。
斯威夫特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]
}
恐怕沒有比其他檢查所有方向的項目設置,然後在除了需要一個 – heximal
@heximal所有視圖控制器限制他們的方式,請參閱接受的答案,這是相當不錯的,不需要更新我所有的VC。 –