質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

6766閲覧

Swift 初回起動は画面の向きを縦(ポートレート)で固定して特定のViewControllerで回転(ランドスケープ)を許容したい。

fathy

総合スコア254

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2019/07/02 09:17

編集2019/07/03 11:53

#試したこと
info.plist内で起動時の画面を縦固定し, AppDelegate で application(application:supportedInterfaceOrientationsForWindow)内で

Swift

1func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow) -> Int { 2 3 return Int(UIInterfaceOrientationMask.allButUpsideDown.rawValue) 4 }

としていますが, どの画面も回転しません。

参考:iPhone6Plusと初回起動時の画面向きについて

各ViewController には

Swift

1override var supportedInterfaceOrientations: UIInterfaceOrientationMask{ 2 return .allButUpsideDown 3 } 4 5 override var shouldAutorotate: Bool{ 6 return true 7 }

として回転を許容しています。

以下の記事についても試しましたが,効果はありませんでした。

check the box "Requires full screen" in the General Targets setting
iOS9 supportedInterfaceOrientationsForWindowが呼び出されなくなる

表題の通り,LaunchScreenを固定して回転させる方法をご教授いただきたいです。
よろしくお願いいたします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

hayabusabusash

2019/07/04 02:28

逆にinfo.plistで回転を許容する設定にしておいて、 回転させたくないViewControllerには回転させないようにした場合はどうでしょうか? NavigationController管理下のViewControllerは気をつけた方がいいみたいです。 ( https://qiita.com/YuwUnknown/items/876f5f1c1734ce6a7651 )
fathy

2019/07/04 02:44 編集

ご回答いただきありがとうございます! 当初そのような実装をしていたのですが、launchscreenの制御はコードからではできず、info.plist内でのみ制限できるとのことでした。 参考: https://qiita.com/kenmaz/items/1704152ef38c35bfe9fe launchscreenの横向きの画像も用意しておくのが一番早いですかね…。
hayabusabusash

2019/07/04 03:02

ちょっと手元でやってみます。 質問についてですが、LaunchScreenは縦のみサポートして 一部のViewControllerでは横向きもサポートするということがやりたいことになりますか?
fathy

2019/07/04 03:05

仰る通りです。 お手数おかけしますがお願い致します。
guest

回答1

0

ベストアンサー

ほとんど質問者様と同じですが以下のようにしてみてはどうでしょうか?

まずプロジェクト単位(info.plist)のデバイスの向きの設定はPortraitのみに設定します。
これでLaunchScreenは縦のみがサポートされます。

次にAppDelegate内で以下の記述をしてデバイスの向きの設定を変更します。

Swift

1class AppDelegate: UIResponder, UIApplicationDelegate { 2 3 var window: UIWindow? 4 5 6 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { 7 // Override point for customization after application launch. 8 return true 9 } 10 11 func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask { 12 return UIInterfaceOrientationMask.allButUpsideDown 13 } 14} 15 16

そして縦のみをサポートしたいViewControllerは以下のようにして縦のみをサポートするようにします。

Swift

1class ViewController: UIViewController { 2 3 override var shouldAutorotate: Bool { 4 return true 5 } 6 7 override var supportedInterfaceOrientations: UIInterfaceOrientationMask { 8 return .portrait 9 } 10 11 override func viewDidLoad() { 12 super.viewDidLoad() 13 } 14} 15

回転をサポートするViewControllerでは以下のようにします。

Swift

1class ViewController: UIViewController { 2 3 override var shouldAutorotate: Bool { 4 return true 5 } 6 7 override var supportedInterfaceOrientations: UIInterfaceOrientationMask { 8 return .allButUpsideDown 9 } 10 11 override func viewDidLoad() { 12 super.viewDidLoad() 13 } 14} 15

ただこれらがUINavigationController、またはUITabBarControllerの管理下にある場合
そのままでは各ViewControllerで設定した値が反映されないので対策する必要があります。

UINavigationControllerを使って試していたので、
以下のようなExtensionを定義して各ViewControllerの設定を反映させるようにしました。

Swift

1extension UINavigationController { 2 3 open override var shouldAutorotate: Bool { 4 guard let viewController = self.visibleViewController else { return true } 5 return viewController.shouldAutorotate 6 } 7 8 open override var supportedInterfaceOrientations: UIInterfaceOrientationMask { 9 guard let viewController = self.visibleViewController else { return .all } 10 return viewController.supportedInterfaceOrientations 11 } 12}

投稿2019/07/04 03:41

hayabusabusash

総合スコア767

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

fathy

2019/07/04 04:09

本当にありがとうございます。 2日もハマっていました笑。 AppDelegateの application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) の部分が application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow) となってwindowが余分についていたことが問題のようでした。 古いコードを参照してコピペしたことを反省すると同時に,hayabusabusash様にこんなミスにお付き合いさせてしまい申し訳無いです。 本当にありがとうございます。
hayabusabusash

2019/07/04 04:28

よかったです! そういったコードは関数名だけ見てあとはXcodeの補完に任せた方が間違わなくいいですよ! 今まで画面の向きを制限する機会はなかったので勉強になりました。こちらこそありがとうございます。
fathy

2019/07/04 04:34

今後からは気をつけます。 そう言って頂けてよかったです。 ありがとうございました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問