iOS Disabling Landscape LaunchScreen.storyboard

余生颓废 提交于 2019-12-11 01:54:38

问题


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.


回答1:


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




回答2:


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")
    }

}


来源:https://stackoverflow.com/questions/50756091/ios-disabling-landscape-launchscreen-storyboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!