I have an LaunchScreen.storybaord
that shows a logo (textual so it is orientation agnostic). The app starts always in portrait, but it has certain view controllers that allow landscape mode (so making the app portrait only is not an option).
What I want is that the launch screen always comes up in portrait. So holding the phone in landscape during app start should result in seeing the logo rotated (as if looking at a view controller that does not support landscape)
Is there a way to tell the storyboard to be portrait only?
I tried to use size classes traits, but this way I can only address left OR right landscape. Since there is also an intro screen for first time users that animate the logo from launch screen, the logo rotation depends on which direction the phone was put in landscape.
There are several approaches to achieve this. My preferred one though is to specify in Info.plist
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
</array>
And override this after app finished launching inside AppDelegate
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return [.allButUpsideDown]
}
Source: https://developer.apple.com/library/archive/technotes/tn2244/_index.html#//apple_ref/doc/uid/DTS40009012-CH1-ALLOWING_YOUR_APP_TO_ROTATE_INTO_PORTRAIT_ORIENTATION_AFTER_LAUNCH
Click on project navigator, then click on the target -> select the project > click on general -> deployment info then you will see below view
check here device orientation that you checked during project creation and mark the orientation that you want. It will work.
OR
you can use below code in ViewDidLoad:
NotificationCenter.default.addObserver(self, selector: #selector(orientationChanged), name: Notification.Name("UIDeviceOrientationDidChangeNotification"), object: nil)
Then, create one function like below
@objc func orientationChanged() {
if(UIDeviceOrientationIsLandscape(UIDevice.current.orientation)){
print("landscape")
}
if(UIDeviceOrientationIsPortrait(UIDevice.current.orientation)){
print("Portrait")
}
}