在UITabBarController中强制特定的viewcontroller在landscape中

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在UITabBarController中强制特定的viewcontroller在landscape中相关的知识,希望对你有一定的参考价值。

我在UITabBarController中有5个导航控制器。 UITabBarController中的一个Navigationcontroller有一个Viewcontroller总是将其他所有肖像都映射到..

-(BOOL)shouldAutorotate
{
    return [self.topViewController shouldAutorotate];
}

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

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return [self.topViewController preferredInterfaceOrientationForPresentation];
}

上面的代码我放在NavigationController子类中。

-(BOOL)shouldAutorotate
{
    return NO;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAll;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeLeft;
}

上面的代码我放入了Landscape Controller。

我有Base作为UIViewController子类,我把这个代码

-(BOOL)shouldAutorotate
{
    return NO;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAll;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    if ([self isKindOfClass:[LanscapeViewController class]])
        return UIInterfaceOrientationLandscapeLeft;
    else
        return UIInterfaceOrientationPortrait;
}

如果我提出那个viewcontroller它很好地显示屏幕只有景观。但是,如果我将ViewController推送到Portrait中的遗骸中。

答案

您是否在ViewController中为Portrait Mode编写了下面的代码。

    -(BOOL)shouldAutorotate
{
    return NO;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown);
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortraitUpsideDown | UIInterfaceOrientationPortrait;
}
另一答案

看看这篇文章。分别是http://www.klecker.de/photo/index.php?option=com_content&task=view&id=148&Itemid=213Presenting Navigation Controller in Landscape mode is not working ios 6.0

我在标签栏应用程序中这样做了,但我没有强制旋转其中一个选项卡的直接根视图控制器,但是它的一个子视图。我不确定这是否适用于标签栏的一个视图控制器。

另一答案

创建一个UIViewController类别来检查当前的ViewController,

#import <UIKit/UIKit.h>

@interface UIViewController (Utils)

+(UIViewController*) currentViewController;

@end

#import "UIViewController+Utils.h"

@implementation UIViewController (Utils)

+(UIViewController*) findBestViewController:(UIViewController*)vc {

    if (vc.presentedViewController) {

        // Return presented view controller
        return [UIViewController findBestViewController:vc.presentedViewController];

    } else if ([vc isKindOfClass:[UISplitViewController class]]) {

        // Return right hand side
        UISplitViewController* svc = (UISplitViewController*) vc;
        if (svc.viewControllers.count > 0)
            return [UIViewController findBestViewController:svc.viewControllers.lastObject];
        else
            return vc;

    } else if ([vc isKindOfClass:[UINavigationController class]]) {

        // Return top view
        UINavigationController* svc = (UINavigationController*) vc;
        if (svc.viewControllers.count > 0)
            return [UIViewController findBestViewController:svc.topViewController];
        else
            return vc;

    } else if ([vc isKindOfClass:[UITabBarController class]]) {

        // Return visible view
        UITabBarController* svc = (UITabBarController*) vc;
        if (svc.viewControllers.count > 0)
            return [UIViewController findBestViewController:svc.selectedViewController];
        else
            return vc;

    } else {

        // Unknown view controller type, return last child view controller
        return vc;

    }

}

+(UIViewController*) currentViewController {

    // Find best view controller
    UIViewController* viewController = [UIApplication sharedApplication].keyWindow.rootViewController;
    return [UIViewController findBestViewController:viewController];

}

@end

Appdelegate.m导入上面的UIViewcontroller类别,导入要在Landscape中显示的ViewController并实现方向方法

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    if ([[UIViewController currentViewController] isKindOfClass:[YourLandScapeViewController class]]) {

        return UIInterfaceOrientationMaskLandscape; //Orientation for LandScapeViewcontroller

    }
    return UIInterfaceOrientationMaskPortrait; // rest All ViewController
}

以上是关于在UITabBarController中强制特定的viewcontroller在landscape中的主要内容,如果未能解决你的问题,请参考以下文章

强制 UITabBarItem 图像为特定大小

如何在 UITabBarController 中获取特定 UIViewController 的索引

如何在UITabBarController中获取特定UIViewController的索引

如何在特定索引处显示 UITabBarController?

防止自定义 UITabBarController 更改选项卡

从UITabBarController内部呈现UINavigationController