我几乎没有使用 presentViewController:animated: 方法模态呈现的 View Controller 。 我不想让应用程序在 iPad 上显示它们时旋转,因为它们都使用 FXBlurView 模糊了背景。旋转起来很麻烦,而且有时旋转呈现的 View 会弄乱呈现的 View 。
我需要支持 iOS6、7 和 8,除了私有(private) API setOrientation: 方法之外找不到任何解决方案。
最佳答案
我找到的唯一解决方案是创建一个导航 Controller 子类并在该导航 Controller 中包含模态视图 Controller 。
这意味着您将必须呈现导航 Controller ,该 Controller 将具有您最初想要呈现的 Root View Controller 。
这是非旋转(纵向)导航 Controller 的示例实现:
@implementation NonRotatingNavigationController
- (BOOL)shouldAutorotate {
return NO;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationPortrait;
}
@end
您也可以“询问”显示的 View Controller ,而不是决定支持哪个方向。
像这样:
@implementation NonRotatingNavigationController
- (BOOL)shouldAutorotate {
return [[[self viewControllers] lastObject] shouldAutorotate];
}
- (NSUInteger)supportedInterfaceOrientations {
return [[[self viewControllers] lastObject] supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return [[[self viewControllers] lastObject] preferredInterfaceOrientationForPresentation];
}
@end
编辑(添加了替代解决方案)
另一种解决方案是添加一个包含 View Controller 的单独窗口。
这次您必须在 View Controller 本身内部实现旋转方法,并将其设置为新窗口的 Root View Controller 。
这是窗口创建代码(重要的是窗口将由某人拥有 - 例如原始 View Controller 中的 strong 属性 - 否则它将被释放,你将什么也看不到):
@interface ViewController ()
@property (nonatomic, strong) UIWindow *modalWindow;
@end
@implementation ViewController
- (IBAction)buttonTapped:(id)sender {
NonRotatingViewController *vc = [[self storyboard] instantiateViewControllerWithIdentifier:@"NonRotatingViewController"];
UIWindow *modalWindow = [[UIWindow alloc] initWithFrame:self.view.window.frame];
modalWindow.windowLevel = UIWindowLevelAlert;
modalWindow.backgroundColor = [UIColor clearColor];
[modalWindow setRootViewController:vc];
[modalWindow makeKeyAndVisible];
self.modalWindow = modalWindow;
}
@end
关于ios7 - iOS8 iOS7 和 iOS6 阻止模态呈现的 View Controller 的旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26830554/