1

そのため、次のようにして、コードの一部でデバイスの向きを強制的に変更しようとしています。

if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortrait ||
        [[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortraitUpsideDown){
        [[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationLandscapeLeft animated:NO];
    }

しかし、向きは変わりませんでした。どうしてこれなの?基本的に現在の向きは縦向きで、横向きに強制したいです

4

4 に答える 4

5

Inder Kumar Rathoreの提案がうまくいけば、それは素晴らしいことです。しかし、ドキュメントでは、orientationプロパティが読み取り専用であると説明されているため、それが機能する場合、将来的に機能することを信頼できるかどうかはわかりません(Appleが賢明なことをしてこれを変更しない限り、方法に関係なく、方向の変更を強制しますユーザーは現在デバイスを持っていますが、これは明らかな機能上の必要性です)。

別の方法として、viewDidLoadに挿入された次のコードは、正常に(そしてやや不思議なことに)方向を強制します(roronoa zorroが推奨するようにshouldAutorotateToInterfaceOrientationを既に変更していると仮定します)。

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

明らかに、これは、ユーザーが現在デバイスを縦向きに保持している場合に行われます(したがって、shouldAutorotateToInterfaceOrientationは横向きのみに設定されており、ユーザーがデバイスを縦向きモードに保持している場合、このルーチンは横向きにシフトします)。shouldAutorotateToInterfaceOirentationが縦向きのみに設定されている場合は、UIDeviceOrientationIsPortraitをUIDeviceOrientationIsLandscapeと交換するだけです。

何らかの理由で、メインウィンドウからビューを削除してから再度追加すると、shouldAutorotateToInterfaceOrientationにクエリが実行され、方向が正しく設定されます。これがAppleが承認したアプローチではないことを考えると、おそらくそれを使用することを控えるべきですが、それは私にとってはうまくいきます。あなたのマイレージは異なる場合があります。しかし、このSOの議論は、他の手法にも言及しています。

于 2012-05-20T06:28:45.407 に答える
2

デバイスが横向きの場合に強制的に縦向きモードにする方法はありません。逆の場合も同様です。できることは、アプリケーションを横向きまたは縦向きの特定のモードにすることだけです。次の方法でそれを行うことができます。

  1. 実装する必要がありますshouldAutorotateToInterfaceOrientation

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    { 
        // Return YES for supported orientations
        return UIInterfaceOrientationIsLandscape(interfaceOrientation);
    }
    
  2. pList を編集しています。

    キーUIInterfaceOrientationを plist に追加し、必要に応じてそのプロパティをUIInterfaceOrientationLandscapeLeft,に変更します..UIInterfaceOrientationLandscapeRightUIInterfaceOrientationPortraitUpsideDown

于 2012-05-20T05:14:11.803 に答える
1

編集

これは iOS 7 では機能しない可能性がありますが、以前のバージョンの iOS では機能していたため、この回答に反対票を投じても意味がありません。質問は2年前に聞かれ、その時に答えました。


[[UIDevice currentDevice] setOrientation: UIDeviceOrientationLandscapeLeft];
于 2012-05-20T05:13:55.217 に答える