限制选项卡栏视图控制器中的旋转 [英] Restrict Rotation in Tab Bar View Controllers

查看:138
本文介绍了限制选项卡栏视图控制器中的旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新2

在我的UITabBarController子类中,我尝试添加此项:

In my UITabBarController subclass I tried adding this:

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

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

}

现在,每当我选择一个标签项时,设备就会旋转完美的肖像模式。但是,现在我可以在选择(a)时旋转设备,设备将旋转到横向模式。如何阻止设备旋转?

Now, everytime I select a tab item, the device rotates to portrait mode perfectly. However, now I can rotate the device while (a) is selected, and the device will rotate to landscape mode. How can I stop the device from rotating?

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

I believe this method in my tab controller subclass is what is causing this:

- (BOOL)shouldAutorotate {

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

    return [self.viewControllers.lastObject shouldAutorotate];
}

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

If I return 'NO' I cannot rotate when I'm in the view controller, but when I select it, it does not automatically rotate to portrait. If I return 'YES' I can rotate when I'm in the view controller, but when I select it, it automatically rotates to portrait.

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

I have a custom Tab Bar Controller in my app with the following hierarchy:

UITabBarController
    |
    UINavigationController
    |  |
    |  UIViewController(a)
    |
    UINavigationController
    |  |
    |  UIViewController(b)
    |
    UINavigationController
       |
       UIViewController(c)

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

I want View Controller (a) to only be able to be viewed in portrait mode, and View Controllers (b) and (c) to be viewable in all orientations but upside down. Now, I can do this with each view controller individually, but my issue comes in when I am in (b) in landscape mode, and select the tab item for (a), a is then displayed in landscape mode, which does not look good. How can I make sure the tab bar (or the view controller) checks to see if the to-be-selected view controller can be viewed in the current orientation?

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

If needed, here is my code for (a) that restricts it to portrait mode on its own:

- (BOOL) shouldAutorotate
{
    return NO;
}

- (NSUInteger) supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

更新

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

I have added this to view controller (a), but I get a black square in the middle of my view, and the title in the nav bar is no longer centered.

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


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


}


推荐答案

由于iOS 7.0 UITabBarController支持通过进行方向转发 - (NSUInteger)tabBarControllerSupportedInterfaceOrientations:(UITabBarController *)tabBarController

Since iOS 7.0 UITabBarController supports orientation forwarding via - (NSUInteger)tabBarControllerSupportedInterfaceOrientations:(UITabBarController *)tabBarController.

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

It works great until you switch to the other tab while being at interface orientation that this particular tab does not support.

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

Unfortunately tabbar controller does not force the rotation in that case and the only way is to use private API.

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

Note: I use attemptRotationToDeviceOrientation to rotate interface to device orientation when switching back from portrait only tab to tab that supports any orientation.

这是我解决问题的方法:

This is my approach to problem:

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

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

I put example app on Github at https://github.com/pronebird/TabBarOrientationExample

这篇关于限制选项卡栏视图控制器中的旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆