我有一个仅限 SwiftUI iPhone 的应用程序,其方向锁定为纵向,效果很好。但是,当在 iPad 上运行相同的应用程序时(在模拟模式下;iPad 不是目标),锁定不起作用:UI 随设备旋转,这在无意中破坏了 UI。它在模拟器和实际设备上都失败了。
我可以使用只有一个 View 的空 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()
}
}
}
有趣的是:我正在使用 Xcode 12.5 和 Swift 5。
最佳答案
关于ios - 如何为在 iPad 上运行的 iPhone SwiftUI 应用程序锁定纵向方向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67553648/