使用 Xcode 11 时,SwiftUI 视图在小窗口中呈现而不是全屏呈现 [英] SwiftUI view being rendered in small window instead of full-screen when using Xcode 11

查看:49
本文介绍了使用 Xcode 11 时,SwiftUI 视图在小窗口中呈现而不是全屏呈现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I recently created a new SwiftUI project using the beta for Xcode 12. Then I tried to open this project in the non-beta Xcode 11, and after updating the code to use a SwiftUI 1.0-style AppDelegate, I was able to build and run the app. The issue is that now that I have moved to Xcode 11, the app renders inside of a small frame instead of taking up the whole screen.

这是一个简化的例子:

Xcode 12 与 Xcode 11

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

My simplified view's code is as follows:

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、构建设置等之间的任何区别.


I have tried creating a new Xcode 11 project (which renders properly) and comparing its contents to my own project's, but so far I have not been able to find any difference between its AppDelegate, SceneDelegate, ContentView, build settings, etc.

为了让 SwiftUI 在 Xcode 11 项目中全屏呈现,是否应该更改选项?

推荐答案

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

这可以通过执行以下操作来解决:

This can be resolved by doing the following:

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

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

In the storyboard, add a new View Controller.

在您的应用目标设置中,找到应用图标和启动图像"部分.

In your app target's settings, find the "App Icons and Launch Images" section.

从下拉菜单中选择您的启动屏幕:

Select your launch screen from the dropdown:

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

Now run your application and the SwiftUI view will fill up the whole screen!

这篇关于使用 Xcode 11 时,SwiftUI 视图在小窗口中呈现而不是全屏呈现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆