【问题标题】:iOS6 OrientationiOS6 方向
【发布时间】:2012-09-22 17:24:50
【问题描述】:

我有一个关于 iOS 6 Orientation 的问题。这是我的文件 https://www.dropbox.com/s/f8q9tghdutge2nu/Orientations_iOS6.zip

在这个示例代码中,我想让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 可以有多个方向吗?

谢谢!

【问题讨论】:

  • 我今天找到了解决方案。在您的 CustomNavigationController 中设置 - (NSUInteger)supportedInterfaceOrientations { return [[self topViewController] supportedInterfaceOrientations]; } 然后在 MasterViewController 上设置 - (NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskPortrait; }
  • 如果您找到了问题的答案,您应该将您的答案作为答案发布,并将其标记为正确,没关系。

标签: ios orientation landscape ios6 portrait


【解决方案1】:

谢谢!布伦南,
我还在我的博客中收集了其他方法。
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

【讨论】:

  • 赞成,因为阅读您的帖子使我最终解决了我遇到的 ios6 设备旋转问题。我希望我的应用程序仅是纵向的,除非在呈现全屏视频时。我的解决方案是在 info.plist 中将 Supported Orientations 设置为 Portrait,覆盖标签栏子类以返回纵向/纵向蒙版/自动旋转 NO。这是因为标签栏是父视图控制器!谢谢。
【解决方案2】:

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

下面是我使用的代码。

- (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;
}

【讨论】:

    猜你喜欢
    • 2012-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 2012-11-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多