简体   繁体   English

Xcode 11 beta swift ui 预览不显示

[英]Xcode 11 beta swift ui preview not showing

Just playing with Swift UI basic app and the preview canvas is not showing even though I'm in canvas mode.只是玩 Swift UI 基本应用程序和预览 canvas 即使我处于 canvas 模式也没有显示。 App runs, and I have this little snippet what am I missing?应用程序运行,我有这个小片段我错过了什么?

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

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

To open the preview canvas, just go to Editor -> Canvas要打开预览画布,只需转到Editor -> Canvas

Or do Alt + Command + Enter或者执行Alt + Command + Enter

If your macOS version is below 10.15, you will get the below error如果您的 macOS 版本低于 10.15,则会出现以下错误

在此处输入图片说明

Update mac and try the same.更新 mac 并尝试相同的操作。

To preview and interact with views from the canvas in Xcode, ensure your Mac is running on Catalina MacOS .要在 Xcode 的画布中预览视图并与之交互,请确保您的 Mac 在Catalina MacOS上运行。

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

Please check apple document in the following URL https://developer.apple.com/documentation/xcode_release_notes/xcode_11_beta_2_release_notes请检查以下 URL 中的苹果文档https://developer.apple.com/documentation/xcode_release_notes/xcode_11_beta_2_release_notes

Xcode 11 beta supports development with SwiftUI. Xcode 11 beta 支持使用 SwiftUI 进行开发。

Note笔记

Tools for SwiftUI development are only available when running on macOS Catalina 10.15 beta. SwiftUI 开发工具仅在 macOS Catalina 10.15 beta 上运行时可用。

From the editor:来自编辑:

Top-Right corner of the editor, and then:编辑器的右上角,然后:

在此处输入图片说明


From the menu从菜单

Editor -> Canvas

在此处输入图片说明


Shortcuts快捷方式

Open Canvas打开画布

⌥ option + ⌘ command + ↩ return ⌥ 选项+ ⌘ 命令+ ↩ 返回

Refresh canvas刷新画布

⌥ option + ⌘ command + P ⌥ 选项+ ⌘ 命令+ P


A note about the macOS 关于 macOS  的说明

In the mac environment, you can search for any menu option under the Help menu, then it will open the exact menu path you are looking for.在mac环境下,你可以在Help菜单下搜索任意菜单选项,就会打开你要找的确切菜单路径。

For example, searching for can vas result in this:例如,搜索can vas 结果如下:

在此处输入图片说明

Additionally, if you are using macOS system older than beta version like 10.14 you can use Xcode playground to preview with following code.此外,如果您使用的 macOS 系统早于 10.14 等 beta 版本,您可以使用 Xcode playground 预览以下代码。

import PlaygroundSupport
import SwiftUI

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

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

I'm running Xcode 12.2 on Catalina 10.15.7, and I not only need to make sure Canvas is selected but then I need to select Create Preview from the Editor menu.我在 Catalina 10.15.7 上运行 Xcode 12.2,我不仅需要确保选择了 Canvas,而且还需要从 Editor 菜单中选择 Create Preview。

Xcode 编辑器菜单

In Xcode 11 Beta 6 , there is a known issue and workaround for views that do not have their certain flags set (see below).Xcode 11 Beta 6 中,对于未设置特定标志的视图存在一个已知问题和解决方法(见下文)。

If this applies to you, and you are unable to set the corresponding flags, you may need to remove the #if debug flag around ContentView_Previews until the issue is fixed in a future Xcode release.如果这适用于您,并且您无法设置相应的标志,您可能需要删除ContentView_Previews周围的#if debug标志,直到该问题在未来的 Xcode 版本中得到修复。

See the Xcode 11 Beta 6 Release Notes :请参阅Xcode 11 Beta 6 发行说明

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.在不具有项目中定义的预览提供-DDEBUG为集OTHER_SWIFT_FLAGS和在包裹#if DEBUG不会在画布显示。 That happens, for example, in Objective-C apps which that never had occasion to specify OTHER_SWIFT_FLAGS .例如,在从未有机会指定OTHER_SWIFT_FLAGS Objective-C 应用程序中会发生这种情况。 (51138834) (51138834)

Workaround: Remove the #if DEBUG and #endif from around the PreviewProvider declaration.解决方法:PreviewProvider声明周围删除#if DEBUG#endif

Alt + Command + Enter Alt + 命令 + 输入

Will solve the issue会解决问题

Check this xcode_11_beta_5_release_notes https://developer.apple.com/documentation/xcode_release_notes/xcode_11_beta_5_release_notes .检查这个 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)对于 Xcode 11 beta 5,预览仅在 macOS Catalina 10.15 beta 5 上可用,只有 Xcode 11 beta 5 支持在 macOS Catalina 10.15 beta 5 上预览。(52059562)

I have restarted the Xcode and machine and it's worked for me.我已经重新启动了 Xcode 和机器,它对我有用。 You can try this for many problems.您可以尝试解决许多问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM