我试图在我的应用程序中仅强制在横向模式下使用一个视图, 我在打电话

override func shouldAutorotate() -> Bool {
    print("shouldAutorotate")
    return false
}

override func supportedInterfaceOrientations() -> Int {
    print("supportedInterfaceOrientations")
    return Int(UIInterfaceOrientationMask.LandscapeLeft.rawValue)
}

override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
    return UIInterfaceOrientation.LandscapeLeft
}

The view is launched in the portrait mode, and keep rotating when I change the device orientation.
The shouldAutorotate is never called.
Any help would be appreciated.

推荐答案

这可能对其他人有用,我找到了一种方法来强制视图以横向模式启动:

将其放入viewDidLoad()中:

let value = UIInterfaceOrientation.landscapeLeft.rawValue
UIDevice.current.setValue(value, forKey: "orientation")

override var shouldAutorotate: Bool {
    return true
}

Ios相关问答推荐

第三方框架的iOS隐私声明

DriverKit驱动程序中可能存在IOBufferMemoyDescriptor泄漏

无法下载并安装带有Xcode 15.0的iOS 17.0模拟器运行时

发生错误.请稍后重试.在 App Store 连接中

在最新版本的 iOS 中,设备上的Localized.strings文件发生了什么变化?

NSOperation 有延迟 - 它是异步的还是同步的?

如何在 SwiftUI 中只显示一次随机数组的项目?

包装在 AnyView 中时 UIViewControllerRepresentable 无法正常工作

通过隐藏和显示视图在 iMessage 应用程序中使用不同的视图

通过 tuist/XcodeProj 以编程方式将文件添加到本机目标

按钮在 SwiftUI 中没有采用给定的高度和宽度

将视图(例如 Text())不透明度设置为 0 或将背景设置为 Color.clear 会导致它不被绘制

如何设置imageView的圆角半径?

将图像保存到 Documents 目录并检索邮箱附件

iOS:应用程序在安装应用程序时未征求用户许可.每次都获得 kCLAuthorizationStatusNotDetermined - Objective-c & Swift

导航控制器自定义过渡动画

如何在 OS X 或 iOS(不使用格式塔)中确定运行时的操作系统版本?

所有小部件的Flutter 填充?

是否可以将 Socket.io 与 AWS Lambda 一起使用?

从故事板导航到其他 Swift 1.2 文件时 Xcode 6.3 崩溃