禁用 iOS 横向启动画面 LaunchScreen.storyboard

11

我有一个 LaunchScreen.storyboard ,显示一个标志(文本形式,因此不受方向影响)。应用程序始终以纵向模式启动,但它有某些视图控制器允许横向模式(因此使应用程序仅限纵向模式不是选项)。

我想要的是,启动屏幕始终以纵向模式出现。因此,在应用程序启动期间将手机保持为横向可能会导致看到旋转的标志(就像查看不支持横向的视图控制器一样)

是否有一种方法可以告诉storyboard仅支持纵向模式?

我尝试使用大小类特征,但这样我只能解决左侧或右侧的横向问题。由于还有针对首次用户的介绍屏幕,这会从启动屏幕动画标志,标志旋转取决于手机在横向时放置方向。


请参考此 Stack Overflow 帖子:https://dev59.com/nV4b5IYBdhLWcg3wlihP - Tibin Thomas
3个回答

18

有几种方法可以实现这一点。然而,我更喜欢的是在Info.plist中指定。

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

在AppDelegate内应用完成启动后覆盖此方法。

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    return [.allButUpsideDown]
}

来源: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


对我来说它不起作用。 - Parth

0
除了Sunil的回复之外,您还需要从“构建设置”中删除该值。输入图像描述

-2

点击项目导航器,然后点击目标 -> 选择项目 > 点击常规 -> 部署信息,然后您将看到下面的视图

enter image description here

检查您在项目创建期间选择的设备方向,并标记您想要的方向。它会起作用。

或者

您可以在ViewDidLoad中使用以下代码:

NotificationCenter.default.addObserver(self, selector: #selector(orientationChanged), name:  Notification.Name("UIDeviceOrientationDidChangeNotification"), object: nil)

然后,创建一个如下所示的函数

@objc func orientationChanged() {

    if(UIDeviceOrientationIsLandscape(UIDevice.current.orientation)){

        print("landscape")
    }

    if(UIDeviceOrientationIsPortrait(UIDevice.current.orientation)){

        print("Portrait")
    }

}

2
viewDidLoad 在启动屏幕视图控制器中没有被调用,也无法提供自定义类。当操作系统呈现故事板时,应用程序未运行。 - sofacoder
1
将应用程序设置为纵向模式无效,因为您不能拥有任何支持横向的视图控制器。 - sofacoder

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接