简体   繁体   English

锁定屏幕方向为纵向 - ios swift

[英]Lock screen Orientation to portrait - ios swift

I am creating an application with support only for portrait mode.我正在创建一个仅支持纵向模式的应用程序。 I don't want Landscape or Portrait upside down.我不希望横向或纵向颠倒。 I tried some code.我尝试了一些代码。 Which allows me to lock in portrait mode.这使我可以锁定纵向模式。

I am using navigationcontroller and presentviewcontroller.我正在使用navigationcontroller 和presentviewcontroller。 Now my problem is: 1. If I rotate my device upside down and open my application it opens in upside down mode which is wrong.现在我的问题是: 1. 如果我将设备倒置并打开我的应用程序,它会以倒置模式打开,这是错误的。 2. I click some button and enter to presentviewcontroller it returns to portrait mode. 2.我点击一些按钮并进入presentviewcontroller它返回到纵向模式。

I want all the navigationcontroller and presentviewcontroller in portrait mode我想要纵向模式下的所有导航控制器和呈现视图控制器

My codes:我的代码:

I set device orientarion portrait in Target -> General -> Deployment Info -> Portrait我在Target -> General -> Deployment Info -> Portrait中设置了设备方向肖像

In my appdelagate.swift:在我的 appdelagate.swift 中:

func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
        return UIInterfaceOrientationMask.Portrait
    }

In my First view controller.在我的第一个视图中,controller。 Which is basically child of Navigation Controller这基本上是 Navigation Controller 的孩子

override func shouldAutorotate() -> Bool {


            return false
        }

        override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
            return [UIInterfaceOrientationMask.Portrait ]
        }

Edit 1:编辑1:

Also I set StoryBoard -> ViewController -> Attribute Inspector -> Orientation -> Portrait我还设置StoryBoard -> ViewController -> Attribute Inspector -> Orientation -> Portrait

Edit 2:编辑2:

My settings file我的设置文件在此处输入图像描述

Go To Target --> General and set Orientation Mode to Portrait. 转到目标 - >常规并将方向模式设置为纵向。

在此输入图像描述

Also check info.plist. 另请查看info.plist。 Make sure Supported Interface Orientations contains only one value(Portrait). 确保Supported Interface Orientations仅包含一个值(纵向)。 Sometime it removes from settings but not updated in plist file. 有时它从设置中删除但未在plist文件中更新。

在此输入图像描述

If your app is not supporting split screen view than you should check the option to Require full screen. 如果您的应用不支持分屏视图,则应选中“要求全屏”选项。 This fixed my issue with portrait mode. 这解决了我的肖像模式问题。 在此输入图像描述

If this does not resolve issue add Supported interface orientations Key of Array type in your info.plist. 如果这不能解决问题,请在info.plist中添加支持的接口方向数组键类型。 And String item to that array with value Portrait (bottom home button) 和String项目到该数组的值为Portrait(底部主页按钮)

在此输入图像描述

For me the only working solution was to add:对我来说,唯一可行的解决方案是添加:

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    .portrait
}

in the app's delegate class, as indicated in the question itself.在应用程序的代表 class 中,如问题本身所示。

I'm using SwiftUI without any storyboard file, even for the splashscreen.我正在使用没有任何storyboard文件的 SwiftUI 文件,即使对于启动画面也是如此。

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

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