强制旋转UIViewController [英] Force Rotate UIViewController

查看:120
本文介绍了强制旋转UIViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我已经放弃尝试解决我一直在控制旋转的问题而不调用我的 shouldAutorotateToInterfaceOrientation:方法,因为每个人都在几年来一直把它说成是一个iOS错误。

So, I've given up on trying to solve an issue I've been having where my controller will rotate without calling my shouldAutorotateToInterfaceOrientation: method, because everyone for a few years has been stating it as an iOS bug.

现在我需要强制旋转我的 UIViewController 。有没有办法可以做到这一点,因为现在已经删除了 UIDevice 实例方法,我不知道如何强制旋转我的控制器。

Now I need to just force rotate my UIViewController. Is there a way I can do that, since the UIDevice instance method has been removed now and I don't know what to do to force rotate my controller.

推荐答案

我不确定您所说的 UIDevice 方法已被删除,但以下对我有用(这确实使用 UIDevice orientation 方法)。最重要的是,如果您的视图只接受横向视图,则可以使用以下命令强制iOS更改方向。这很奇怪,但它确实有效。我很抱歉我不能赞美原作者,但是曾经在StackOverflow上遇到过这个问题:

I'm not sure which UIDevice method that you are saying has been removed, but the following has worked for me (this does use the UIDevice orientation method). Bottom line, if you have a view that only accepts landscape, you can use the following to force iOS to change the orientation. It's curious, but it works. I apologize that I can't give credit to the original author, but once came across this elsewhere on StackOverflow:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

- (void)forceLandscape
{
    // make sure shouldAutorotateToInterfaceOrientation is configured to accept only landscape

    if (UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation]))
    {
        UIWindow *window = [[UIApplication sharedApplication] keyWindow];
        UIView *view = [window.subviews objectAtIndex:0];
        [view removeFromSuperview];
        [window addSubview:view];
    }
}

相当于强制纵向模式(假设,当然,你的 shouldAutorotateToInterfaceOrientation 只接受肖像):

The equivalent to force portrait mode (assuming, of course, your shouldAutorotateToInterfaceOrientation accepts only portrait):

- (void)forcePortrait
{
    // make sure shouldAutorotateToInterfaceOrientation is configured to accept only portrait

    if (UIDeviceOrientationIsLandscape([[UIDevice currentDevice] orientation]))
    {
        UIWindow *window = [[UIApplication sharedApplication] keyWindow];
        UIView *view = [window.subviews objectAtIndex:0];
        [view removeFromSuperview];
        [window addSubview:view];
    }
}

这篇关于强制旋转UIViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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