我正在开发这个应用程序,我需要所有的 View Controller ,但需要一个纵向 View Controller 。 一个特别的单一 View Controller ,我需要它能够旋转到手机的任何方向。
为此,我以模态方式呈现它(未嵌入 NavigationController)
所以(例如)我的结构是这样的:
- 窗口 - 纵向
- Root View Controller (UINavigationController - 纵向)
- 主视图 Controller (UIViewController - 纵向)
- 细节 View Controller (UIViewController - 纵向)
- >。
- .
- .
- 模态视图 Controller (UIVIewController - 全部)
- 主视图 Controller (UIViewController - 纵向)
- Root View Controller (UINavigationController - 纵向)
现在,每当我在横向位置关闭模态视图 Controller 时,我的父 View Controller 也会旋转,即使它不支持该方向。
应用程序中的所有 UIViewControllers
和 UINavigaionControllers
都继承自实现了这些方法的相同通用类:
override func supportedInterfaceOrientations() -> Int
{
return Int(UIInterfaceOrientationMask.Portrait.toRaw())
}
我的模态视图 Controller 再次覆盖了这个方法,它看起来像这样:
override func supportedInterfaceOrientations() -> Int
{
return Int(UIInterfaceOrientationMask.All.toRaw())
}
更新 1
看起来这只发生在 iOS8 Beta 上。 有人知道 View Controller 的旋转是否发生了变化,还是这只是测试版中的一个错误?
最佳答案
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if ([self.window.rootViewController.presentedViewController isKindOfClass: [SecondViewController class]])
{
SecondViewController *secondController = (SecondViewController *) self.window.rootViewController.presentedViewController;
if (secondController.isPresented)
return UIInterfaceOrientationMaskAll;
else return UIInterfaceOrientationMaskPortrait;
}
else return UIInterfaceOrientationMaskPortrait;
}
对于 Swift
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow) -> Int {
if self.window?.rootViewController?.presentedViewController? is SecondViewController {
let secondController = self.window!.rootViewController.presentedViewController as SecondViewController
if secondController.isPresented {
return Int(UIInterfaceOrientationMask.All.toRaw());
} else {
return Int(UIInterfaceOrientationMask.Portrait.toRaw());
}
} else {
return Int(UIInterfaceOrientationMask.Portrait.toRaw());
}
}
有关更多详细信息,请查看此 link
关于ios - 解除模态视图 Controller 时如何保持呈现 View Controller 的方向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24229677/