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

时间:2015-01-20 11:55:49

标签: ios objective-c uitableview uinavigationcontroller uiinterfaceorientation

我在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;
}

上面的代码我输入了横向控制器

我将Base作为UIViewController子类,因为我把这段代码

-(BOOL)shouldAutorotate
{
    return NO;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAll;
}

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

如果我提出那个视图控制器它很好地显示屏幕只有风景。但是,如果我将 ViewController 推送到肖像中。

3 个答案:

答案 0 :(得分:1)

您是否在ViewController中为肖像模式编写了以下代码。

    -(BOOL)shouldAutorotate
{
    return NO;
}

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

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortraitUpsideDown | UIInterfaceOrientationPortrait;
}

答案 1 :(得分:0)

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

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

答案 2 :(得分: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
}