tabBarController和navigationControllers在横向模式中,第二集

Mas*_*aro 6 iphone landscape uitabbarcontroller uiviewcontroller

我有一个UITabBarController,每个选项卡处理一个不同的UIViewController,根据需要推送堆栈的新控制器.在其中两个选项卡中,当达到特定控制器时,我需要能够旋转iPhone并以横向模式显示视图.经过艰苦的努力,我发现必须继承UITabBarController以覆盖shouldAutorotateToInterfaceOrientation.但是,如果我只是在实现中返回YES,则会出现以下不良副作用:

旋转iPhone时,每个标签中的每个控制器都会自动进入横向模式.

甚至覆盖shouldAutorotateToInterfaceOrientation在每个控制器中返回NO不起作用:当iPhone旋转时,控制器处于横向模式.

我在子类UITabBarController中实现了如下的shouldAutorotateToInterfaceOrientation:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if([self selectedIndex] == 0 || [self selectedIndex] == 3)
        return YES;

    return NO;
}
Run Code Online (Sandbox Code Playgroud)

因此,只有我感兴趣的两个选项卡实际上支持横向模式.有没有办法在特定选项卡的堆栈上支持特定控制器的横向模式?

我尝试过没有成功的事情

(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

if([self selectedIndex] == 0 || [self selectedIndex] == 3)
{   
   if ([[self selectedViewController] isKindOfClass: [landscapeModeViewController class]])
           return YES;
    }

     return NO;
Run Code Online (Sandbox Code Playgroud)

}

另外,我尝试使用委托方法didSelectViewController,但没有成功.任何帮助是极大的赞赏.谢谢.

Zar*_*ony 7

这是UITabBarController的扩展,它将调用委托shouldAutorotateToInterfaceOrientation给当前选定的子控制器.使用此扩展,您不再需要继承UITabBarController,您可以shouldAutorotateToInterfaceOrientation在控制器中使用,就像人们期望的那样.

的UITabBarController + Autorotate.h:

#import <UIKit/UIKit.h>

@interface UITabBarController (Autorotate)
@end
Run Code Online (Sandbox Code Playgroud)

的UITabBarController + Autorotate.m:

#import "UITabBarController+Autorotate.h"

@implementation UITabBarController (Autorotate)

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    UIViewController *controller = self.selectedViewController;
    if ([controller isKindOfClass:[UINavigationController class]])
        controller = [(UINavigationController *)controller visibleViewController];
    return [controller shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}

@end
Run Code Online (Sandbox Code Playgroud)


Ric*_*ton 4

这对我有用:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    if(self.selectedIndex == 0 && [[[self.viewControllers objectAtIndex:0] visibleViewController] isKindOfClass:[MyViewController class]])
        return YES;
    else
        return NO;
}
Run Code Online (Sandbox Code Playgroud)