使用 Xcode 11 时,SwiftUI 视图在小窗口而不是全屏中呈现

问题描述 投票:0回答:5

我最近使用 Xcode 12 的测试版创建了一个新的 SwiftUI 项目。然后我尝试在非测试版 Xcode 11 中打开这个项目,并在更新代码以使用 SwiftUI 1.0 风格的 AppDelegate 后,我能够构建和运行应用程序。问题是 现在我已经迁移到 Xcode 11,应用程序在一个小框架内呈现,而不是占据整个屏幕。

这是一个简化的示例:

Xcode 12 与 Xcode 11


我的简化视图的代码如下:

struct ContentView: View {
    var body: some View {
        VStack {
            Text("Hello World!")
        }
    }
}

应用程序委托:

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        // Called when a new scene session is being created.
        // Use this method to select a configuration to create the new scene with.
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }
}

场景代表:

class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    var window: UIWindow?

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
        // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
        // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).

        // Create the SwiftUI view that provides the window contents.
        let contentView = ContentView()

        // Use a UIHostingController as window root view controller.
        if let windowScene = scene as? UIWindowScene {
            let window = UIWindow(windowScene: windowScene)
            window.rootViewController = UIHostingController(rootView: contentView)
            self.window = window
            window.makeKeyAndVisible()
        }
    }

我尝试创建一个新的 Xcode 11 项目(可以正确渲染)并将其内容与我自己的项目进行比较,但到目前为止我还没有发现其 AppDelegate、SceneDelegate、ContentView、构建设置等之间的任何差异。

为了让 SwiftUI 在 Xcode 11 项目中全屏渲染,是否应该更改一个选项?

ios swift xcode swiftui
5个回答
38
投票

这是因为 Xcode 11 项目缺少启动屏幕。

可以通过执行以下操作来解决此问题:

  • 右键单击项目名称,选择新建文件…,然后选择故事板

  • 在故事板中,添加一个新的视图控制器。

  • 在应用程序目标的设置中,找到“应用程序图标和启动图像”部分。

  • 从下拉列表中选择您的启动屏幕:

现在运行您的应用程序,SwiftUI 视图将填满整个屏幕!


29
投票

只需添加

<key>UILaunchScreen</key>
<dict/>

进入信息列表为我解决了这个问题。


2
投票

解决此问题的方法是在 Info.plist 中添加 LaunchScreen 条目,如下所示:

<key>UILaunchScreen</key>
<dict>
    <key>UIColorName</key>
    <string>LaunchBackground</string>
</dict>

LaunchBackground 必须对应于 Assets.xcassets 中的颜色。


0
投票

@ahimsa das提到过

如果我在“启动屏幕文件”字段中输入随机字符串。有用!任何像“osd8nfcosd”这样的随机乱码仍然有效。奇怪哈哈。

这很奇怪,但我认为这与 iOS 上的缓存有关。通过在启动屏幕文件名中输入随机字符串(在应用程序的目标设置中),xcode 会在您的项目中搜索故事板文件,但由于它找不到任何内容,因此它默认为启动屏幕的缓存图像(如果我是这样,请纠正我)错误)这将是创建的第一个

launchscreen.storyboard
文件的图像

(在我的例子中,这发生在我使用 UIKit 并想以编程方式制作一个应用程序时,所以我删除了

launchscreen.storyboard
以及所有对它的引用,这当然导致了一个小的
UIWindowScene


-4
投票

使用

ignoresSafeArea

struct ContentView: View {
    var body: some View {
        VStack {
            Text("Hello World!")
        }
        .ignoresSafeArea()
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.