ios - 如何为在 iPad 上运行的 iPhone SwiftUI 应用程序锁定纵向方向?
问题描述
我有一个 SwiftUI iPhone 专用应用程序,其方向锁定为纵向,效果很好。但是,当相同的应用程序在 iPad 上运行时(在仿真模式下;iPad 不是目标),锁定不起作用:UI 会随着设备旋转,这是无意的,因为它会破坏 UI。它在模拟器和实际设备上失败。
我可以使用只有一个视图的空 SwiftUI 项目来重现此问题:
struct ContentView: View {
var body: some View {
Text("Hello, world!")
}
}
iPhone按预期工作:
iPad 不遵守方向锁定:
我的 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。
解决方案
推荐阅读
- tensorflow - 使用 GCP 训练的模型进行推理?
- vba - 从父表单上的文本框条目中过滤子表单数据
- sql - 内连接查询返回不同的值
- windows - pyttsx - 郎集
- ios - SwiftUI 视图大小(父/子关系)
- javascript - 如何为 React Native 组件预处理获取的 redux 数据?
- wpf - 如何以编程方式使用 ModernWPF
- reactjs - 在 React Native 中将函数传递给功能组件
- javascript - 使用来自服务器的数据自动提供 html 页面?
- bash - 用于检查 openshift/kubernetes pod 是否准备就绪的 Bash 脚本