【发布时间】: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 项目中全屏呈现,是否应该更改某个选项?
【问题讨论】: