我有几个视图控制器,使用presentViewController:animated:方法以模态方式呈现。因为它们都使用了FXBlurView,具有模糊的背景,所以我不希望在iPad上呈现时允许应用程序旋转,这会导致混乱,有时候旋转已经呈现的视图会破坏呈现视图的排版。
我需要支持iOS6、7和8,并且除了使用私有API setOrientation:方法外,我找不到任何解决方案。
我有几个视图控制器,使用presentViewController:animated:方法以模态方式呈现。因为它们都使用了FXBlurView,具有模糊的背景,所以我不希望在iPad上呈现时允许应用程序旋转,这会导致混乱,有时候旋转已经呈现的视图会破坏呈现视图的排版。
我需要支持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