Mar*_*rco 7 xcode ios swift swiftui
我最近使用 Xcode 12 的 beta 创建了一个新的 SwiftUI 项目。然后我尝试在非 beta Xcode 11 中打开这个项目,在更新代码以使用 SwiftUI 1.0-style AppDelegate 后,我能够构建和运行应用程序。问题是,现在我已经转移到 Xcode 11,该应用程序在一个小框架内呈现,而不是占据整个屏幕。
这是一个简化的示例:
Xcode 12 与 Xcode 11
我的简化视图的代码如下:
struct ContentView: View {
var body: some View {
VStack {
Text("Hello World!")
}
}
}
Run Code Online (Sandbox Code Playgroud)
应用委托:
@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)
}
}
Run Code Online (Sandbox Code Playgroud)
场景委托:
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()
}
}
Run Code Online (Sandbox Code Playgroud)
我已经尝试创建一个新的 Xcode 11 项目(它可以正确呈现)并将其内容与我自己的项目的内容进行比较,但到目前为止我还没有找到它的 AppDelegate、SceneDelegate、ContentView、构建设置等之间的任何区别。
为了让 SwiftUI 在 Xcode 11 项目中全屏呈现,是否应该更改选项?
这是因为 Xcode 11 项目缺少启动屏幕。
这可以通过执行以下操作来解决:
右键单击您的项目名称,选择New File...,然后选择Storyboard。
在故事板中,添加一个新的视图控制器。
在您的应用程序目标设置中,找到“应用程序图标和启动图像”部分。
从下拉列表中选择您的启动屏幕:
现在运行您的应用程序,SwiftUI 视图将填满整个屏幕!
| 归档时间: |
|
| 查看次数: |
1534 次 |
| 最近记录: |