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

28
我最近使用Xcode 12的beta版创建了一个新的SwiftUI项目。然后我尝试在非beta版的Xcode 11中打开此项目,并更新代码以使用SwiftUI 1.0-style的AppDelegate,我能够构建和运行应用程序。问题是,现在我已经转移到Xcode 11,应用程序在一个小框架内呈现,而不是占据整个屏幕。
以下是一个简化的示例:
Xcode 12 vs. Xcode 11

SwiftUI app taking up whole screen SwiftUI rendering in small frame, not full screen


我的简化视图代码如下:

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项目中全屏渲染?
5个回答

35

这是由于Xcode 11项目缺少启动屏幕引起的。

可以通过以下方法解决:

  • 右键单击项目名称,选择新建文件...,并选择Storyboard

  • 在Storyboard中,添加一个新的View Controller。

  • 在您的应用程序目标设置中,找到"App Icons and Launch Images"部分。

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

    enter image description here

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


1
我在使用 Xcode 12.3 的时候遇到了这个问题。非常奇怪,显然这是一个 bug,但您提供的解决方案起作用了。 - Wolf McNally
非常感谢,我的朋友,我不知道为什么这不是默认情况! - Romain Sickenberg
你真是个救星。XCode 12.5.1仍然存在这个bug,而XCode 13 beta则没有。 - Matej Balantič
Xcode 13.1 对我来说有这个 bug,这个修复了它。 - Michael Ellis
我在13.2.1版本中仍然遇到这个错误。这个解决方法仍然有效。谢谢! - Mathieson
显示剩余2条评论

28

仅需添加

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

将信息属性列表(info plist)添加进去解决了我的问题。 在此输入图片描述


更改后必须进行清理构建。可以工作,谢谢。 - Raginmari
1
哇 - 对我也起作用了。在迁移到完整的SwiftUI项目后,我清理Info.plist时过于激进了。但是为什么? - Sébastien Stormacq
这个解决方案在从UiKit迁移到SwiftUI项目时运行良好,但我不知道根本原因是什么。有人可以帮忙吗? - Ali
1
这是修复。 顺便提一下,Xcode 14.3.1在iOS 16和17的模拟器中仍然存在问题。 - Hugo Alonso

1
解决方法是在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


-3
使用ignoresSafeArea:
struct ContentView: View {
    var body: some View {
        VStack {
            Text("Hello World!")
        }
        .ignoresSafeArea()
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接