2012-12-26 42 views
4

在我的项目中,我只允许肖像旋转,但对于一个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:这是非常乱,我可以” t旋转StatusBar

我真的很喜欢使用标准方法,如shouldAutorotateToInterfaceOrientation,但我不知道如何。任何人?

+0

设置故事板中该特定视图控制器的方向是什么? – SangamAngre

+0

如果您将属性检查器中的方向设置设置为继承。我不能设置肖像或只是风景,我想这两个支持。 –

+0

什么是为您的项目设置的支持方向? – SangamAngre

回答

-1

enter image description here

实现在所有控制器和返回上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)); 
} 
+1

这也是我的第一个猜测......在项目设置中设置所有方向并且应用旋转......但是这些方法永远不会在iOS6上调用,因此您无法在单独的每个视图控制器上控制它......而是调用supportedInterfaceOrientations方法只是在viewDidLoad之后...但不知何故,即使我返回UIInterfaceOrientationPortrait在它没有任何变化,我可以旋转视图控制器到所有方向... –

+3

在IOS 6中不起作用 – JSA986

+0

与我同样的问题,我想支持所有的方向对于单个视图,所有其他视图必须仅支持纵向方向。应用程序在iOS5中运行良好,但在iOS6中它即使在我不支持风景方向时也会旋转。请帮忙 – iMemon

2

子类为您的屏幕,导航控制器需要旋转。

在.M

// 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中的视图控制器只能看到有父母或根控制器,用于旋转方法

0

你就不能调用在viewDidLoad中和viewWillAppear中的shouldAutoRotateToInterfaceOrientation像这样:

[self shouldAutorotateToInterfaceOrientation]; 

如果要调用的方法及其在您的ViewController

4

在您的应用程序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 
{ 
} 

添加这个,然后在每一个视图控制器(.M添加此方法

-(void)canRotate 
{ 
    // just define the method, no code required here 
} 

文件)您想要提供旋转的位置。您也可以在这里包括-(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; 
    } 
}