ios - 如何为在 iPad 上运行的 iPhone SwiftUI 应用程序锁定纵向方向?

标签 ios swift iphone ipad swiftui

我有一个仅限 SwiftUI iPhone 的应用程序,其方向锁定为纵向,效果很好。但是,当在 iPad 上运行相同的应用程序时(在模拟模式下;iPad 不是目标),锁定不起作用:UI 随设备旋转,这在无意中破坏了 UI。它在模拟器和实际设备上都失败了。
我可以使用只有一个 View 的空 SwiftUI 项目来重现这个问题:

struct ContentView: View {
    var body: some View {
        Text("Hello, world!")
    }
}
iPhone 按预期工作:
iPhone in portrait mode
iPhone in landscape mode
iPad 不支持方向锁定:
iPad in portrait mode
iPad in landscape mode
我的 Info.plist 中有以下内容,但没有成功。 UI 仍然在 iPad 上旋转。
<key>UISupportedInterfaceOrientations</key>
<array>
    <string>UIInterfaceOrientationPortrait</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
    <string>UIInterfaceOrientationPortrait</string>
</array>
然后我定义了一个 AppDelegate。仍然没有成功,UI 在 iPad 上不断旋转。
class AppDelegate: NSObject, UIApplicationDelegate {
    func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
        return UIInterfaceOrientationMask.portrait
    }
}

@main
struct orientationApp: App {
    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}
有趣的是:
  • 当我启用 iPad 作为目标时,方向锁开箱即用。但是,我不想为 iPad 提供该应用程序。我只想为故意在 iPad 上安装 iPhone 应用程序的用户修复 UI。
  • 当我在 iPhone 上测试 AppDelegate 方法时,即使我在 Info.plist 中允许所有设备方向,它也能工作,所以它似乎做正确的事情(只是不在 iPad 上)。

  • 我正在使用 Xcode 12.5 和 Swift 5。

    最佳答案

    enter image description here
    取消勾选 Supports multiple windows为我解决了问题

    关于ios - 如何为在 iPad 上运行的 iPhone SwiftUI 应用程序锁定纵向方向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67553648/

    相关文章:

    iphone - 索引在 TableView 中不起作用

    ios - 如何在 iOS 中获取当前时间而不是设备时间

    ios - 在 GameScene 中显示 UIAlertController (SpriteKit/Swift)

    swift - 呈现 Alert Swift 后的功能

    xcode - 当我尝试将另一个值保存到原始变量时,Swift 保存数据并崩溃

    iphone - Facebook SDK 登录对话框出现两次

    ios - 如何设置子类 UIButton 的突出显示标题颜色?

    ios - 与多个开发人员进行代码签名

    ios - 如何在运行时确定应用程序是分发给内部测试人员还是外部测试人员?

    ios - 左侧的 UITableView 滚动指示器