简体   繁体   English

iOS禁用Landscape LaunchScreen.storyboard

[英]iOS Disabling Landscape LaunchScreen.storyboard

I have an LaunchScreen.storybaord that shows a logo (textual so it is orientation agnostic). 我有一个可以显示徽标的LaunchScreen.storybaord (文本,因此与方向无关)。 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 我的首选方法是在Info.plist中指定

<key>UISupportedInterfaceOrientations</key>
<array>
    <string>UIInterfaceOrientationPortrait</string>
</array>

And override this after app finished launching inside AppDelegate 并在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 来源: 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: 您可以在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")
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM