Xcode 11 beta swift ui 预览未显示

Xcode 11 beta swift ui preview not showing

只是玩 Swift UI 基本应用程序,即使我处于 canvas 模式,预览 canvas 也没有显示。应用程序运行,我有这个小片段我错过了什么?

#if DEBUG
struct ContentView_Previews : PreviewProvider {
    static var previews: some View {
       ContentView()
    }
}
#endif

要在 Xcode 中预览 canvas 的视图并与之交互,请确保您的 Mac 是 运行 macOS 10.15 beta。

此外,如果您使用的 macOS 系统早于 beta 版本,例如 10.14,您可以使用 Xcode playground 通过以下代码进行预览。

import PlaygroundSupport
import SwiftUI

    struct ContentView : View {
        var body: some View {
            Text("Hello World")
        }
    }
 PlaygroundPage.current.liveView = UIHostingController(rootView: ContentView())

要在 Xcode 中预览 canvas 的视图并与之交互,请确保您的 Mac 在 Catalina Mac 上是 运行 OS.

https://developer.apple.com/tutorials/swiftui/creating-and-combining-views

请查看苹果文档如下URL https://developer.apple.com/documentation/xcode_release_notes/xcode_11_beta_2_release_notes

Xcode 11 beta supports development with SwiftUI.

Note

Tools for SwiftUI development are only available when running on macOS Catalina 10.15 beta.

勾选这个 xcode_11_beta_5_release_notes https://developer.apple.com/documentation/xcode_release_notes/xcode_11_beta_5_release_notes

With Xcode 11 beta 5, previews are only available on macOS Catalina 10.15 beta 5, and only Xcode 11 beta 5 supports previews on macOS Catalina 10.15 beta 5. (52059562)

也许有人在找这个: Editor -> Canvas (Alt + Command + Enter)

Xcode 11 Beta 6 中,存在一个已知问题和针对未设置特定标志的视图的解决方法(见下文)。

如果这适用于您,并且您无法设置相应的标志,您可能需要删除 ContentView_Previews 周围的 #if debug 标志,直到问题在未来得到解决 Xcode 发布.

参见Xcode 11 Beta 6 Release Notes

Preview providers that are defined in projects which don’t have -DDEBUG set for OTHER_SWIFT_FLAGS and wrapped in #if DEBUG don’t display in the canvas. That happens, for example, in Objective-C apps which that never had occasion to specify OTHER_SWIFT_FLAGS. (51138834)

Workaround: Remove the #if DEBUG and #endif from around the PreviewProvider declaration.

要打开预览 canvas,只需转至 Editor -> Canvas

或者Alt + Command + Enter

如果您的 macOS 版本低于 10.15,您将收到以下错误

更新 mac 并尝试相同的方法。

来自编辑:

Top-Right编辑一角,然后:


从菜单中

Editor -> Canvas


快捷方式

打开Canvas

⌥ 选项 + ⌘ 命令 + ↩ return

刷新canvas

⌥ 选项 + ⌘ 命令 + P


关于 macOS

的注释

在mac环境下,您可以在帮助菜单下搜索任何菜单选项,然后它会打开您要查找的确切菜单路径。

例如,搜索 canvas 结果如下:

我是 Catalina 10.15.7 上的 运行 Xcode 12.2,我不仅需要确保 Canvas 是 selected,而且我还需要select 从编辑器菜单创建预览。

我已经重新启动了 Xcode 和机器,它对我有用。 很多问题都可以试试这个。

Alt + Command + Enter

将解决问题