2012-09-22 61 views
2

我有一个问题关于iOS 6的Orientation.Here是我的文件 https://www.dropbox.com/s/f8q9tghdutge2nu/Orientations_iOS6.zipiOS6的方向

在此示例代码,我想使MasterViewController只有一个纵向和DetailViewController为纵向,横向。

我知道iOS 6的方向是由最顶级的控制器控制的。

所以我定制一个UINavigationController(CustomNavigationController),设置supportedInterfaceOrientations和shouldAutorotate在该类。

-(NSUInteger)supportedInterfaceOrientations{ 
    if([[self topViewController] isKindOfClass:[DetailViewController class]]){ 
     return UIInterfaceOrientationMaskAllButUpsideDown; 
    }else{ 
     return UIInterfaceOrientationMaskPortrait; 
    } 
} 

-(BOOL)shouldAutorotate 
{ 
    return YES; 
} 

一切除了当DetailViewController在横向方向按返回键,MasterViewController会显示正横罚款。

我可以让MasterViewController始终显示纵向和DetailViewController可以有多个方向?

谢谢!

+1

我今天找到解决方案。 (NSUInteger)supportedInterfaceOrientations { return [[self topViewController] supportedInterfaceOrientations];在您的自定义导航控制器中设置此选项为 。 } 然后设置该上MasterViewController - (NSUInteger)supportedInterfaceOrientations { 返回UIInterfaceOrientationMaskPortrait; } – Hanpo

+0

如果您发现问题的答案,则应将答案作为答案发布,并将其标记为正确,没关系。 – Raspu

回答

1

我按照您对该问题的评论中的建议进行了此项工作。问题是默认的UINavigatonController没有使用顶级视图控制器的值,所以你需要通过创建一个基类并在故事板中将其设置为基类来覆盖它。

下面是我使用的代码。

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

我也有一个基类的我的视图控制器,其余为默认的行为,以便使用纵向方向。我可以在支持多于纵向方向的任何视图控制器中覆盖iOS 5和6的这些方法。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

- (NSUInteger)supportedInterfaceOrientations { 
    return UIInterfaceOrientationMaskPortrait; 
} 

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { 
    return UIInterfaceOrientationPortrait; 
} 

- (BOOL)shouldAutorotate { 
    return FALSE; 
} 
3

谢谢!布伦南,
我也收集其他方式来做到这一点在我的博客。
http://blog.hanpo.tw/2012/09/ios-60-orientation.html

下面是其他两种方式。

1.增加一个类别来的UINavigationController

@implementation UINavigationController (Rotation_IOS6) 

    -(BOOL)shouldAutorotate 
    { 
     return [[self.viewControllers lastObject] shouldAutorotate]; 
    } 

    -(NSUInteger)supportedInterfaceOrientations 
    { 
     return [[self.viewControllers lastObject] supportedInterfaceOrientations]; 
    } 

    - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation 
    { 
     return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation]; 
    } 

    @end 

2.Swap方法实现(通过spoletto制造)

https://gist.github.com/3725118

+0

Upvote因为阅读您的文章导致我终于修复了我遇到的ios6设备旋转问题。我希望我的应用只能显示肖像,除了呈现全屏视频时。我的解决方案是将支持的方向设置为info.plist中的纵向,覆盖标签栏子类以返回纵向方向/纵向方向遮罩/自动旋转编号。这工作,因为标签栏是父视图控制器!谢谢。 – maz