2013-04-05 61 views
2

我的应用程序包含两个表视图控制器。在第一个我希望视图能够左右旋转(除了肖像模式),但在第二个表视图控制器(我从第一个表中点击单元格后导航到)我想要它只能在肖像模式下查看。我试过这个代码,但它没有工作,它一直在旋转。限制某些视图的自转

- (NSUInteger)supportedInterfaceOrientations { 
    return UIInterfaceOrientationMaskPortrait; 
} 

- (BOOL) shouldAutorotate { 
    return NO; 
} 

注意:我确实从项目目标的摘要选项卡启用了左/右/纵向方向。任何修复?

+0

检查此“http://stackoverflow.com/questions/12772749/support-different-orientation-for-only-one-view-ios-6” – Nandha 2013-04-05 13:27:46

回答

2

创建的UINavigationController一个类别,它包括以下方法:

(两者为iOS 6和iOS 5作品)

- (BOOL)shouldAutorotate 
    { 
     return self.topViewController.shouldAutorotate; 
    } 

    - (NSUInteger)supportedInterfaceOrientations 
    { 
     return self.topViewController.supportedInterfaceOrientations; 
    } 

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation { 
     return [self.topViewController shouldAutorotateToInterfaceOrientation:toInterfaceOrientation]; 
    } 

然后在控制器

首先实现这些方法:

- (BOOL)shouldAutorotate { 
    return YES; 
} 

- (NSUInteger)supportedInterfaceOrientations { 
    if (RUNNING_IPAD) { 
     return UIInterfaceOrientationMaskAll; 
    } 
    else { 
     return UIInterfaceOrientationMaskAllButUpsideDown; 
    }; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation { 
    if (RUNNING_IPAD) { 
     return YES; 
    } 
    else { 
     return toInterfaceOrientation != UIInterfaceOrientationMaskPortraitUpsideDown; 
    } 
} 

第二个:

- (BOOL)shouldAutorotate { 
    return NO; 
} 

- (NSUInteger)supportedInterfaceOrientations { 
     return UIInterfaceOrientationMaskPortrait; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation { 
     return NO; 
} 

项目的旋转设置应该是这样的:

Settings

+0

谢谢!我确实找到了一个更简单的答案,但我选择了这个,因为它支持ios 5和6,我的只支持6。 – HusseinB 2013-04-05 14:14:12

0

其实我发现我的问题的解决方案:

-(BOOL)shouldAutorotate{ 
    return YES; 
} 

-(NSInteger)supportedInterfaceOrientations{ 
    return UIInterfaceOrientationMaskPortrait; 
} 

这将限制期为纵向即使您在项目的Traget的摘要选项卡中切换左/右方向。