小编Dom*_*nik的帖子

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

我有一个仅适用于 iPhone 的 SwiftUI 应用程序,其方向锁定为纵向,效果非常好。但是,当同一个应用程序在 iPad 上运行时(在模拟模式下;iPad 不是目标),锁定不起作用:UI 随设备旋转,这是无意的,因为它会破坏 UI。它在模拟器和实际设备上都会失败。

我可以使用只有一个视图的空 SwiftUI 项目来重现此问题:

struct ContentView: View {
    var body: some View {
        Text("Hello, world!")
    }
}
Run Code Online (Sandbox Code Playgroud)

iPhone 按预期工作:

iPhone 处于纵向模式 iPhone 处于横向模式

iPad 不尊重方向锁定:

iPad 纵向模式 iPad 横屏模式

我的 Info.plist 中有以下内容,但没有成功。iPad 上的 UI 仍然旋转。

<key>UISupportedInterfaceOrientations</key>
<array>
    <string>UIInterfaceOrientationPortrait</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
    <string>UIInterfaceOrientationPortrait</string>
</array>
Run Code Online (Sandbox Code Playgroud)

然后我定义了一个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 { …
Run Code Online (Sandbox Code Playgroud)

iphone ipad ios swift swiftui

4
推荐指数
1
解决办法
3433
查看次数

标签 统计

ios ×1

ipad ×1

iphone ×1

swift ×1

swiftui ×1