我有几个使用presentViewController:animated:方法以模态形式呈现的视图控制器。我不想让应用程序在iPad上呈现时旋转,因为它们使用FXBlurView都具有模糊的背景,这使旋转变得一团糟,而且因为有时旋转呈现的视图会使呈现的视图变得混乱。
我需要支持iOS6、7和8,并且除了私有API setOrientation:方法之外找不到任何解决方案。
我发现的唯一解决方案是创建一个导航控制器子类,并在此导航控制器中包含模态视图控制器。
这意味着您将必须提供导航控制器,该控制器将具有您最初想要呈现的根视图控制器。
这是非旋转(人像)导航控制器的示例实现:
@implementation NonRotatingNavigationController
- (BOOL)shouldAutorotate {
return NO;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationPortrait;
}
@end
您也可以“询问”显示的视图控制器,而不是确定支持哪种方向。
像这样:
@implementation NonRotatingNavigationController
- (BOOL)shouldAutorotate {
return [[[self viewControllers] lastObject] shouldAutorotate];
}
- (NSUInteger)supportedInterfaceOrientations {
return [[[self viewControllers] lastObject] supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return [[[self viewControllers] lastObject] preferredInterfaceOrientationForPresentation];
}
@end
另一个解决方案是添加一个单独的窗口,其中将包含视图控制器。
这次,您将必须在视图控制器本身内部实现旋转方法,并将其设置为新窗口的根视图控制器。
这是窗口创建代码(重要的是,该窗口将归某人所有-例如,原始视图控制器中的强属性-否则它将被释放,您将一无所获):
@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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句