首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >当前UINavigation控制器支持iOS 6 UITabBarController的方向

当前UINavigation控制器支持iOS 6 UITabBarController的方向
EN

Stack Overflow用户
提问于 2012-09-20 07:57:05
回答 4查看 16.9K关注 0票数 17

我有一个正在更新到iOS 6的iPhone应用程序,它有旋转问题。我有一个16 UINavigationCotrollersUITabBarController。大多数子视图可以在纵向或横向工作,但其中一些只是纵向的。在iOS 6中,事情在不该旋转的时候却在旋转。

我尝试对tabBarController进行子类化,以返回当前导航控制器所选viewController的supportedInterfaceOrienations

- (NSUInteger)supportedInterfaceOrientations{

    UINavigationController *navController = (UINavigationController *)self.selectedViewController;
    return [navController.visibleViewController supportedInterfaceOrientations];
}

这让我更接近了。视图控制器在可见时不会旋转出位置,但如果我在横向中切换选项卡,即使不支持,新选项卡也会在横向中。

理想情况下,应用程序将仅处于当前可见视图控制器的支持方向。有什么想法吗?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2012-09-20 10:48:01

将你的UITabBarController子类化,覆盖这些方法:

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // You do not need this method if you are not supporting earlier iOS Versions
    return [self.selectedViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}

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

-(BOOL)shouldAutorotate
{
    return YES;
}

将你的UINavigationController子类化,覆盖这些方法:

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

-(BOOL)shouldAutorotate
{
    return YES;
}

然后在你的viewControllers中实现这些你不想旋转的方法:

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

-(BOOL)shouldAutorotate
{
    return NO;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

对于您确实想要旋转的viewControllers:

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

    -(NSUInteger)supportedInterfaceOrientations
    {
        return UIInterfaceOrientationMaskAllButUpsideDown;
    }

    -(BOOL)shouldAutorotate
    {
        return YES;
    }

您的tabbarController应添加为应用程序窗口的RootviewController。如果您计划支持默认方向,则除了颠倒方向之外,所有方向都是iPhone的默认方向,那么您不需要执行任何其他操作。如果你想支持倒置或者如果你不想支持另一个方向,那么你需要在app delegate和/或info.plist中设置适当的值。

票数 58
EN

Stack Overflow用户

发布于 2012-09-20 19:45:23

我有一个问题,导航堆栈中的一些视图控制器支持所有的方向,有些只支持纵向,但是UINavigationController返回了所有应用程序支持的方向,这个小技巧帮了我。我不确定这是故意的行为还是什么

@implementation UINavigationController (iOS6OrientationFix)

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

@end
票数 5
EN

Stack Overflow用户

发布于 2012-10-02 20:15:31

我认为这样的东西更好(作为一种分类方法)

-(NSUInteger) supportedInterfaceOrientations {
    if([self.topViewController respondsToSelector:@selector(supportedInterfaceOrientations)])
    {
        return [self.topViewController supportedInterfaceOrientations];
    }
    return UIInterfaceOrientationMaskPortrait;
}

这确保了该方法的实现。如果你不做这个检查,并且方法没有实现(就像在iOS5环境中),那么应用程序就会崩溃!

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12504464

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档