iPhone アプリを作成していますが、ポートレート モードにする必要があるため、ユーザーがデバイスを横に動かしても、自動的に回転しません。これどうやってするの?
10 に答える
特定の 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 で非推奨になったことに注意してください。
Xcode 5 以降
- 左側のサイドバーのプロジェクト ナビゲータでプロジェクトをクリックして、プロジェクト設定を開きます
- [全般] タブに移動します。
- [展開情報]セクションの[デバイスの向き] で、不要なオプションのチェックを外します
Xcode 4 以下
見逃した方のために: プロジェクト設定画面を使用して、アプリ全体の向きを修正できます (すべてのコントローラーでメソッドをオーバーライドする必要はありません)。
サポートされているインターフェイスの向きを切り替えるのと同じくらい簡単です。左側のパネルでプロジェクトをクリックして見つけることができます > アプリのターゲット > [概要] タブ。
クラスからメソッドshouldAutorotateToInterfaceOrientation
を完全に削除することもできます。ローテーションを計画していない場合は、クラスにメソッドを含めることは意味がありません。コードが少ないほど、物事をクリーンに保つことができます。