限制选项卡栏视图控制器中的旋转

Jac*_*cob 2 objective-c rotation uitabbarcontroller uiviewcontroller ios

更新2

在我的UITabBarController子类中,我尝试添加这个:

-(void) tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {

        NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
        [[UIDevice currentDevice] setValue:value forKey:@"orientation"];

}
Run Code Online (Sandbox Code Playgroud)

现在,每次我选择一个标签项时,设备都会完美地旋转到纵向模式.但是,现在我可以在选择(a)时旋转设备,设备将旋转到横向模式.如何阻止设备旋转?

我相信我的tab控制器子类中的这个方法是导致这种情况的原因:

- (BOOL)shouldAutorotate {

    if(self.selectedIndex == 0)
    {
        return NO;
    }

    return [self.viewControllers.lastObject shouldAutorotate];
}
Run Code Online (Sandbox Code Playgroud)

如果我返回"否",当我在视图控制器中时,我无法旋转,但是当我选择它时,它不会自动旋转为纵向.如果我返回"是",我可以在视图控制器中旋转,但是当我选择它时,它会自动旋转为纵向.


我的应用程序中有一个自定义标签栏控制器,具有以下层次结构:

UITabBarController
    |
    UINavigationController
    |  |
    |  UIViewController(a)
    |
    UINavigationController
    |  |
    |  UIViewController(b)
    |
    UINavigationController
       |
       UIViewController(c)
Run Code Online (Sandbox Code Playgroud)

我希望视图控制器(a)只能在纵向模式下查看,而视图控制器(b)和(c)可以在所有方向上查看,但是可以上下颠倒.现在,我可以单独为每个视图控制器执行此操作,但是当我处于横向模式(b)并选择(a)的选项卡项时,我的问题就出现了,然后以横向模式显示a,这看起来不像好.如何确保标签栏(或视图控制器)检查是否可以在当前方向查看要选择的视图控制器?

如果需要,这是我的代码(a)将其自身限制为纵向模式:

- (BOOL) shouldAutorotate
{
    return NO;
}

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

UPDATE

我已将其添加到视图控制器(a)中,但我在视图中间得到一个黑色方块,导航栏中的标题不再居中.

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];


    NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
    [[UIDevice currentDevice] setValue:value forKey:@"orientation"];


}
Run Code Online (Sandbox Code Playgroud)

Ben*_*air 7

由于iOS 7.0 UITabBarController支持方向转发- (NSUInteger)tabBarControllerSupportedInterfaceOrientations:(UITabBarController *)tabBarController.

它工作得很好,直到您切换到另一个选项卡,同时处于此特定选项卡不支持的界面方向.

不幸的是,tabbar控制器在这种情况下不强制旋转,唯一的方法是使用私有API.

注意:attemptRotationToDeviceOrientation当从仅纵向标签切换回支持任何方向的标签时,我用于将界面旋转到设备方向.

这是我解决问题的方法:

static const NSInteger kPortraitOnlyTabIndex = 1;

@interface TabBarController : UITabBarController<UITabBarControllerDelegate>
@end

@implementation TabBarController

- (id)initWithCoder:(NSCoder *)aDecoder {
    if(self = [super initWithCoder:aDecoder]) {
        self.delegate = self;
    }
    return self;
}

- (NSUInteger)tabBarControllerSupportedInterfaceOrientations:(UITabBarController *)tabBarController {
    if(tabBarController.selectedIndex == kPortraitOnlyTabIndex) {
        return UIInterfaceOrientationMaskPortrait;
    }

    return UIInterfaceOrientationMaskAllButUpsideDown;
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    [UIViewController attemptRotationToDeviceOrientation];

    if([self.viewControllers indexOfObject:viewController] == kPortraitOnlyTabIndex) {
        SEL sel = NSSelectorFromString(@"setOrientation:");
        if([[UIDevice currentDevice] respondsToSelector:sel]) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
            [[UIDevice currentDevice] performSelector:sel withObject:(__bridge id)((void*)UIInterfaceOrientationPortrait)];
#pragma clang diagnostic pop
        }
    }
}

@end
Run Code Online (Sandbox Code Playgroud)

我把示例应用程序放在Github上https://github.com/pronebird/TabBarOrientationExample