强制旋转UIViewController

Ril*_*eyE 2 rotation uinavigationcontroller ios

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

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

Rob*_*Rob 7

我不确定UIDevice你说的哪种方法已被删除,但以下内容对我有效(这确实使用了该UIDevice orientation方法).最重要的是,如果您的视图只接受横向视图,则可以使用以下命令强制iOS更改方向.这很奇怪,但它确实有效.我很抱歉我不能赞美原作者,但是曾经在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];
    }
}
Run Code Online (Sandbox Code Playgroud)

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

- (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];
    }
}
Run Code Online (Sandbox Code Playgroud)