36

iPhone アプリを作成していますが、ポートレート モードにする必要があるため、ユーザーがデバイスを横に動かしても、自動的に回転しません。これどうやってするの?

4

10 に答える 10

56

特定の View Controller の向きを無効にするには、 と をオーバーライドする必要がsupportedInterfaceOrientationsありますpreferredInterfaceOrientationForPresentation

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    // Return a bitmask of supported orientations. If you need more,
    // use bitwise or (see the commented return).
    return UIInterfaceOrientationMaskPortrait;
    // return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    // Return the orientation you'd prefer - this is what it launches to. The
    // user can still rotate. You don't have to implement this method, in which
    // case it launches in the current orientation
    return UIInterfaceOrientationPortrait;
}

iOS 6 よりも古いものをターゲットにしている場合は、shouldAutorotateToInterfaceOrientation:メソッドが必要です。はいを返すタイミングを変更することで、その向きに回転するかどうかを決定します。これにより、通常の縦向きのみが許可されます。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
    
    // Use this to allow upside down as well
    //return (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);
}

shouldAutorotateToInterfaceOrientation:iOS 6.0 で非推奨になったことに注意してください。

于 2012-08-02T20:55:04.110 に答える
41

Xcode 5 以降

  • 左側のサイドバーのプロジェクト ナビゲータでプロジェクトをクリックして、プロジェクト設定を開きます
  • [全般] タブに移動します。
  • [展開情報]セクションの[デバイスの向き] で、不要なオプションのチェックを外します

クリックする場所を示すスクリーンショット

于 2014-06-13T12:45:20.143 に答える
28

Xcode 4 以下

見逃した方のために: プロジェクト設定画面を使用して、アプリ全体の向きを修正できます (すべてのコントローラーでメソッドをオーバーライドする必要はありません)。

ここに画像の説明を入力

サポートされているインターフェイスの向きを切り替えるのと同じくらい簡単です。左側のパネルでプロジェクトをクリックして見つけることができます > アプリのターゲット > [概要] タブ。

于 2013-08-17T15:25:28.767 に答える
0

クラスからメソッドshouldAutorotateToInterfaceOrientationを完全に削除することもできます。ローテーションを計画していない場合は、クラスにメソッドを含めることは意味がありません。コードが少ないほど、物事をクリーンに保つことができます。

于 2012-08-03T01:58:47.797 に答える
0

Xcode 8、Xcode 9、Xcode 10 以降

ここに画像の説明を入力

また、Info.plistファイルを変更します。

ここに画像の説明を入力

于 2019-04-25T07:00:15.803 に答える