【问题标题】:SwiftUI view being rendered in small window instead of full-screen when using Xcode 11使用 Xcode 11 时,SwiftUI 视图以小窗口而不是全屏呈现
【发布时间】:2020-11-21 13:05:31
【问题描述】:

我最近使用 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!")
        }
    }
}

AppDelegate:

@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


    【解决方案1】:

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

    这可以通过以下方式解决:

    • 右键点击您的项目名称,选择New File…,然后选择Storyboard

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

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

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

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

    【讨论】:

    • 我刚刚使用 Xcode 12.3 遇到了这个问题。非常奇怪,显然是一个错误,但您的解决方案有效。
    • 非常感谢我的伙计,我不知道为什么默认情况下不是这样!
    • 如果我在“启动屏幕文件”字段中输入随机字符串。有用!任何像“osd8nfcosd”这样的随机乱码,它仍然有效。奇怪的大声笑。
    • 你是救生员。 XCode 12.5.1 仍然有这个错误,而 XCode 13 beta 没有。
    • Xcode 13.1 对我有这个错误并修复了它
    【解决方案2】:

    对此的解决方案是在 Info.plist 中添加一个 LaunchScreen 条目,如下所示 -

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

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

    【讨论】:

      【解决方案3】:

      @ahimsa das 提到

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

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

      (在我的情况下,这发生在我使用 UIKit 并想以编程方式制作应用程序时,所以我删除了 launchscreen.storyboard 和所有对它的引用,当然,这导致了一个小的 UIWindowScene

      【讨论】:

        猜你喜欢
        • 2015-01-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-02
        相关资源
        最近更新 更多