仅允许在一个视图控制器上进行自动旋转
在我的项目中,我只允许纵向旋转,但对于一个ViewController
我想启用横向。 我将此ViewController
呈现为ModalViewController
,我尝试使用方法- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation
或iOS 6方法,如-(NSUInteger) supportedInterfaceOrientations
但实际上没有任何工作。 虽然这些方法被调用,但视图没有旋转。
在此之后,我试图通过听取这些通知的myslef旋转它:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRotate:) name:UIDeviceOrientationDidChangeNotification object:nil];
但即使我能够在方法didRotate
手动旋转view
:它非常混乱,我无法旋转StatusBar
。
我真的想使用像shouldAutorotateToInterfaceOrientation
这样的标准方法,但我不知道如何。 任何人?
在你的app delegate.m中添加它
# pragma mark - Rotation - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window { if ([self.window.rootViewController isKindOfClass:[MVYSideMenuController class]]) { // Get topmost/visible view controller UIViewController *currentViewController = [self.window.rootViewController.childViewControllers lastObject]; // Check whether it implements a dummy methods called canRotate if ([currentViewController respondsToSelector:@selector(canRotate)]) { // Unlock landscape view orientations for this view controller return UIInterfaceOrientationMaskAllButUpsideDown; } } // Only allow portrait (standard behaviour) return UIInterfaceOrientationMaskPortrait; } -(void)canRotate { }
然后添加此方法
-(void)canRotate { // just define the method, no code required here }
在每个要提供旋转的ViewController(.m文件)中。 你也可以在这里包含-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
方法,以便在设备旋转时作出反应:
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]; switch (orientation) { case 1: case 2: //NSLog(@"portrait"); break; case 3: case 4: //NSLog(@"landscape"); break; default: //NSLog(@"other"); break; } }
为需要旋转的屏幕子类化导航控制器。
在他们中
// Older versions of iOS (deprecated) if supporting iOS < 5 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation { return UIInterfaceOrientationIsLandscape(toInterfaceOrientation); } // iOS6 - (BOOL)shouldAutorotate { return YES; } // iOS6 - (NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskLandscape; }
这将覆盖iOS 6摘要页面中设置的旋转方法。
在iOS 6中,视图控制器仅查看父控制器或根控制器的旋转方法
你不能只是在viewDidLoad和viewWillAppear中调用shouldAutoRotateToInterfaceOrientation,如下所示:
[self shouldAutorotateToInterfaceOrientation];
应该在ViewController中调用方法
实现在所有控制器中并返回特定控制器所需的interfaceOrientation
对全部
- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation{ return YES; }
对于景观
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return ((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight)); }
对于肖像
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return ((interfaceOrientation == UIInterfaceOrientationPortrait) || (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)); }