2

そのため、メソッドを呼び出さずにコントローラーが回転するという問題を解決しようとすることをあきらめました。これはshouldAutorotateToInterfaceOrientation:、数年間、誰もが iOS のバグとして述べてきたためです。

ここで、強制的に回転させる必要がありますUIViewControllerUIDeviceインスタンスメソッドが削除され、コントローラーを強制的に回転させる方法がわからないため、それを行う方法はありますか?

4

2 に答える 2

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];
    }
}

ポートレートモードを強制するのと同等です(もちろん、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];
    }
}
于 2012-07-06T20:04:08.483 に答える