我最近使用 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 项目中全屏渲染,是否应该更改一个选项?
解决此问题的方法是在 Info.plist 中添加 LaunchScreen 条目,如下所示:
<key>UILaunchScreen</key>
<dict>
<key>UIColorName</key>
<string>LaunchBackground</string>
</dict>
LaunchBackground 必须对应于 Assets.xcassets 中的颜色。
@ahimsa das提到过
如果我在“启动屏幕文件”字段中输入随机字符串。有用!任何像“osd8nfcosd”这样的随机乱码仍然有效。奇怪哈哈。
这很奇怪,但我认为这与 iOS 上的缓存有关。通过在启动屏幕文件名中输入随机字符串(在应用程序的目标设置中),xcode 会在您的项目中搜索故事板文件,但由于它找不到任何内容,因此它默认为启动屏幕的缓存图像(如果我是这样,请纠正我)错误)这将是创建的第一个
launchscreen.storyboard
文件的图像
(在我的例子中,这发生在我使用 UIKit 并想以编程方式制作一个应用程序时,所以我删除了
launchscreen.storyboard
以及所有对它的引用,这当然导致了一个小的 UIWindowScene
)
使用
ignoresSafeArea
:
struct ContentView: View {
var body: some View {
VStack {
Text("Hello World!")
}
.ignoresSafeArea()
}
}