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

Rya*_*yan 17 rotation uitabbarcontroller orientation uinavigationcontroller ios6

我有一个iPhone应用程序我正在更新到iOS 6有旋转问题.我有UITabBarController16岁UINavigationCotrollers.大多数子视图可以在纵向或横向上工作,但其中一些只是纵向.对于iOS 6,它们不应该旋转.

我尝试将supportedInterfaceOrienationstabBarController 子类化为返回当前navigationController的选定viewController:

- (NSUInteger)supportedInterfaceOrientations{

    UINavigationController *navController = (UINavigationController *)self.selectedViewController;
    return [navController.visibleViewController supportedInterfaceOrientations];
}
Run Code Online (Sandbox Code Playgroud)

这让我更接近.视图控制器在可见时不会旋转到位置,但如果我处于横向和切换选项卡中,即使不支持,新选项卡也将处于横向状态.

理想情况下,应用程序仅在当前可见视图控制器的支持方向上.有任何想法吗?

Dea*_*ids 58

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;
}
Run Code Online (Sandbox Code Playgroud)

UINavigationController重写这些方法的子类:

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

-(BOOL)shouldAutorotate
{
    return YES;
}
Run Code Online (Sandbox Code Playgroud)

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

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

-(BOOL)shouldAutorotate
{
    return NO;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}
Run Code Online (Sandbox Code Playgroud)

对于您想要旋转的viewControllers:

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

    -(NSUInteger)supportedInterfaceOrientations
    {
        return UIInterfaceOrientationMaskAllButUpsideDown;
    }

    -(BOOL)shouldAutorotate
    {
        return YES;
    }
Run Code Online (Sandbox Code Playgroud)

您的tabbarController应添加为应用程序窗口的RootviewController.如果您打算支持默认方向,除iPhone上的所有内容都是默认方式,那么您无需执行任何其他操作.如果您想要支持颠倒或者如果您不想支持其他方向,则需要在app delegate和/或info.plist中设置适当的值.

  • 这几乎对我有用.问题是,当我将标签切换到纵向视图时,我已经处于风景中,它仍处于横向状态.旋转的肖像修复它,它不会旋转回横向,但我仍然需要它在第一次加载时的肖像. (7认同)
  • 我不是在争论这个答案会奏效,但我们不应该这样做.根据Apple的文档,它应该开箱即用.http://developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/ViewControllerCatalog/Chapters/TabBarControllers.html#//apple_ref/doc/uid/TP40011313-CH3-SW26 (2认同)

Min*_*gas 5

我有一个问题,导航堆栈中的一些View控制器支持所有方向,一些只有肖像,但是UINavigationController返回所有应用程序支持的方向,这个小黑客帮助了我.我不确定这是预期的行为还是什么

@implementation UINavigationController (iOS6OrientationFix)

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

@end
Run Code Online (Sandbox Code Playgroud)