使用NavigationController在不同视图上的不同方向 [英] different orientations on different views using a NavigationController

查看:65
本文介绍了使用NavigationController在不同视图上的不同方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对我的NavigationController进行了子类化,并添加了以下代码:

I've subClassed my NavigationController and added this code:

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskLandscapeLeft |UIInterfaceOrientationMaskPortrait;
} 
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeLeft;
}

我有2个视图控制器.在没有旋转功能的情况下,如何在一个模式上强制使用横向模式,在另一个模式上强制使用纵向模式? (所有规则均已在plist中启用)

I have 2 view controllers. How can I force landscape mode on one and portrait mode on another without the ability to rotate? (all ordinations are enabled in the plist)

推荐答案

因此,在经历了许多麻烦和错误之后,我找到了可以使用的解决方案.

So after a lot of trails and error I've found a solution I can work with.

在您的AppDelegate中:

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
    NSUInteger orientations = UIInterfaceOrientationMaskAllButUpsideDown;

    if(self.window.rootViewController){
        UIViewController *presentedViewController = [[(UINavigationController *)self.window.rootViewController viewControllers] lastObject];
        orientations = [presentedViewController supportedInterfaceOrientations];
    }

    return orientations;
}

这会将方向控制权交给根视图控制器.

This will give the control of the orientations to the root view controller.

在您的DataManager中.h(或者您正在使用的任何其他单例也可以通过NSUserDefaults完成.

in your DataManager.h (Or any other singleton you are using.. can be done with NSUserDefaults too.

@property (nonatomic, strong) NSString *currentView;

在您的ViewController.m(根目录)中:

-(NSUInteger)supportedInterfaceOrientations
{

    if([[DataManager sharedDataManager].currentView isEqualToString:@"Trailer"])
    {
        return UIInterfaceOrientationMaskLandscapeRight;
    }
    else if([[DataManager sharedDataManager].currentView isEqualToString:@"Menu"])
    {
        return UIInterfaceOrientationMaskPortrait;
    }

    return UIInterfaceOrientationMaskPortrait;
}

从根ViewController,您可以访问应用程序中所有ViewControlles的所有方向.如果要将一个ViewController限制为横向,将另一个ViewController限制为纵向,则非常有用.您需要添加到每个ViewController的唯一事情是在ViewWillAppear内部,您要为ViewController命名的方式如下:

From the root ViewController you'll have access to all the orientations of all the ViewControlles in your app. Very useful if you want to limit one ViewController to landscape and the other just to portrait. The only thing you'll need to add to each ViewController is inside ViewWillAppear the name you want to give to the ViewController like so:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [DataManager sharedDataManager].currentView = @"Menu";
}

使用此解决方案,您将不会收到像在当前\ dissmis视图解决方案中强制旋转那样迅速在视图之间切换的烦人警告. 也许有一种方法可以更好地识别ViewController,我很想听听如何.如果您发现此解决方案有任何问题,我也很想听听. 谢谢

With this solution you won't get the annoying warnings about switching between views to rapidly like you get with the present \ dissmis view solution for forcing rotation. Maybe there's a way to identify the ViewController in a better way, I'd love to hear how. I would also love to hear if you find any problems with this solution. Thanks

这篇关于使用NavigationController在不同视图上的不同方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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