简体   繁体   English

用于 SwiftUI 预览的 Xcode Canvas 未显示

[英]Xcode Canvas for SwiftUI previews does not show up

I´m trying to get the new Canvas feature from Xcode 11 running, but the Canvas won´t show up.我正在尝试从运行的 Xcode 11 中获取新的 Canvas 功能,但 Canvas 不会出现。 What am I doing wrong?我究竟做错了什么?

This new Xcode feature should show a live preview of my SwiftUI views without running the app.这个新的 Xcode 功能应该可以在不运行应用程序的情况下显示我的 SwiftUI 视图的实时预览。

When you create a custom View with SwiftUI, Xcode can display a preview of the view's content that stays up to date as you make changes to the view's code.当您使用 SwiftUI 创建自定义视图时,Xcode 可以显示视图内容的预览,该内容在您更改视图代码时保持最新。 You define a structure that conforms to the PreviewProvider protocol to tell Xcode what to display.您定义一个符合 PreviewProvider 协议的结构来告诉 Xcode 要显示什么。 Xcode shows the preview in a canvas beside your code. Xcode 在代码旁边的画布中显示预览。

https://developer.apple.com/documentation/swiftui/previews-in-xcode https://developer.apple.com/documentation/swiftui/previews-in-xcode

I just created a new default project (single view app), compiled it and activated 'Editor > Editor and Canvas'.我刚刚创建了一个新的默认项目(单视图应用程序),对其进行了编译并激活了“编辑器 > 编辑器和画布”。 I can navigate to each file in the project, nothing shows up.我可以导航到项目中的每个文件,但没有任何显示。

What else does need to be done?还需要做什么?

You need to be on Catalina macOS version (10.15), as stated in official tutorial官方教程所述,您需要使用Catalina macOS 版本 (10.15)

Be warned: Catalina doesn't support 32-bit applications, some old apps will stop working after update.请注意:Catalina 不支持 32 位应用程序,一些旧应用程序在更新后将停止工作。

you can still see the live view (without Catalina installed ) with the playgrounds.您仍然可以看到带操场的实时视图(未安装 Catalina)。 using UIHostingController .使用UIHostingController

import UIKit
import SwiftUI
import PlaygroundSupport



struct ContentView : View {
    var body: some View {

        Text("Hello World")
            .foregroundColor(Color.blue)
    }
}

// Present the view controller in the Live View window
PlaygroundPage.current.liveView = UIHostingController.init(rootView: ContentView())

在此处输入图片说明

To further add to the other answer, as per the official tutorial from Apple :要进一步添加另一个答案,根据Apple官方教程

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

Unfortunate that I can't run it right now since the beta was just released and I don't have a spare Mac!不幸的是,我现在无法运行它,因为测试版刚刚发布,而且我没有备用 Mac!

Update Mac OS version 10.15 or upper version.更新 Mac OS 版本 10.15 或更高版本。 Update Xcode 11 or upper version.更新 Xcode 11 或更高版本。 After that click Editor > Canvas for code preview.之后,单击 Editor > Canvas 进行代码预览。

Bonus: If you would like to see code preview left side, you can change layout from Menu Icon > Layout > Canvas Right.奖励:如果您想在左侧看到代码预览,您可以从菜单图标 > 布局 > 右侧画布更改布局。 (I shared screenshot) (我分享了截图)

奖金信息截图

The preview only works on MacOS 10.15 Beta预览版仅适用于 MacOS 10.15 Beta

You need to tap on Editor and Canvas Option in Xcode 11.0-Beta, alternatively you can tap on Editor -> Editor and Canvas from top options您需要在 Xcode 11.0-Beta 中点击 Editor and Canvas Option,或者您可以点击顶部选项中的 Editor -> Editor and Canvas

Attached screenshots for refrence.附截图供参考。 图片

图 2

Copy the content of the swift file,复制swift文件的内容,


Move the file to the trash将文件移至回收站


Create the swift file again再次创建 swift 文件


Paste and run your code粘贴并运行您的代码




I tried all these steps, but then realized I did not have my PreviewProvider set up in my file.我尝试了所有这些步骤,但后来意识到我的文件中没有设置PreviewProvider Without this, the preview won't show up.没有这个,预览将不会出现。

Make sure you have it set up like this:确保你有这样的设置:

struct YourView_Previews: PreviewProvider {
    static var previews: some View {
        YourView()
    }
}

Then you can follow the advice of the other answers in this thread and your canvas window should pop right up.然后,您可以按照此线程中其他答案的建议进行操作,您的画布窗口应该会立即弹出。

If you move swiftUI file to a new folder the canvas will not appear and the best solution is to copy all code in the file then remove the file in trash then create a new file and past your code如果您将 swiftUI 文件移动到一个新文件夹,画布将不会出现,最好的解决方案是复制文件中的所有代码,然后在垃圾箱中删除该文件,然后创建一个新文件并通过您的代码

Steps:脚步:

  1. copy file code复制文件代码

  2. delete the file删除文件

  3. create a new file by the same name创建一个同名的新文件

  4. past the code to the new file将代码传递到新文件

This could be the effect if you upgraded Xcode to a higher beta version and got the following error message during the process:如果您将 Xcode 升级到更高的 beta 版本并在此过程中收到以下错误消息,则可能会出现这种效果:

Loading a plug-in failed加载插件失败

The plug-in “com.apple.dt.UVKit” at path “/Applications/Xcode-beta.appDownloads/Xcode-beta.app/Contents/PlugIns/UVKit.framework” could not be loaded.无法加载路径“/Applications/Xcode-beta.appDownloads/Xcode-beta.app/Contents/PlugIns/UVKit.framework”中的插件“com.apple.dt.UVKit”。 The plug-in or one of its prerequisite plug-ins may be missing or damaged.插件或其必备插件之一可能丢失或损坏。 The plug-in or one of its prerequisite plug-ins may be missing or damaged and may need to be reinstalled.插件或其必备插件之一可能丢失或损坏,可能需要重新安装。

The solution seems to be to upgrade the Catalina beta version to the same level.解决方法似乎是将Catalina beta 版升级到同一级别。 Here's the link to Apple's Beta Software Downloads .这是 Apple Beta 版软件下载的链接。

您必须至少安装 macOS 版本(10.15 或更高版本)

Also, make sure this function is called in your SwiftUI file (outside of the actual View struct).此外,请确保在您的 SwiftUI 文件中调用此函数(在实际的View结构之外)。

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView() // Initialize your struct
    }
}

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

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

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

As well as running MacOS Catalina [beta] it seems that you need to have the command line tools installed otherwise it fails and 'pauses' the canvas (this happened with Xcode 11 Beta 2).除了运行 MacOS Catalina [beta] 之外,您似乎还需要安装命令行工具,否则它会失败并“暂停”画布(这发生在 Xcode 11 Beta 2 中)。 To install them run:要安装它们,请运行:

xcode-select --install

Canvas preview only works after update MacOS 10.14 to 10.15 Canvas 预览仅在将 MacOS 10.14 更新到 10.15 后才有效

We have Mac OS Catalina Beta version (10.15)我们有 Mac OS Catalina Beta 版 (10.15)

Upgrade your system and it works.升级您的系统,它可以工作。 :) :)

Check this link .检查此链接

Some important checklist , please check one by one.一些重要的清单,请一一核对。 If all this is done , you can easily get "Editor and Canvas" option.如果这一切都完成了,您就可以轻松获得“编辑器和画布”选项。

  1. Your MacOS version is equal or upper than 10.15您的 MacOS 版本等于或高于 10.15

  2. You are using at least Xcode 11 Beta or later.您至少使用 Xcode 11 Beta 或更高版本。 If you have Xcode 11 but your MacOS is bellow 10.15 , you won't be able to see the option.如果您有 Xcode 11 但您的 MacOS 低于 10.15 ,您将无法看到该选项。

  3. Run the commands from Terminal :从终端运行命令:

sudo xcode-select -s /Applications/Xcode.app/Contents/Developer须藤 xcode-select -s /Applications/Xcode.app/Contents/Developer

and then进而

sudo xcodebuild -license须藤 xcodebuild -license

  1. If check list 1 and 2 is done , go to to the Preferences option and select command Line Tools.如果检查列表 1 和 2 已完成,请转到“首选项”选项并选择“命令行工具”。 Preferences -> Locations and assigning the Command Line Tools to Xcode 11/Beta.首选项 -> 位置并将命令行工具分配给 Xcode 11/Beta。

Most of the time, option 4 is the problem.大多数情况下,选项 4 是问题所在。

Frustrated with what Xcode offers for SwiftUI canvas (and how it does not communicate to you that you need to upgrade to Catalina to have this feature available), I decided to create my own tool: QDesigner .对 Xcode 为SwiftUI画布提供的SwiftUI感到沮丧(以及它如何不告诉您您需要升级到 Catalina 才能使用此功能),我决定创建自己的工具: QDesigner It is an iPad app where you can simply drag & drop SwiftUI controls on iPhone canvas, customize them with a few taps and generate SwiftUI code with a single tap.这是一款 iPad 应用程序,您可以在其中简单地将SwiftUI控件拖放到 iPhone 画布上,轻SwiftUI自定义它们,然后轻点一下即可生成SwiftUI代码。 No Xcode required .无需 Xcode

First Beta of QDesigner is available via TestFlight: QDesigner第一个 Beta 版可通过 TestFlight 获得:
https://testflight.apple.com/join/aSXW5tVp https://testflight.apple.com/join/aSXW5tVp

There is also a companion QDesigner Client app for iPhone, that can be connected with QDesigner running on iPad, to see your UI design on a target device, updated in real-time:还有一个适用于 iPhone 的配套QDesigner Client应用程序,可以与在 iPad 上运行的QDesigner连接,以在目标设备上查看您的 UI 设计,并实时更新:
https://testflight.apple.com/join/DQv0DBJU https://testflight.apple.com/join/DQv0DBJU

I hope it'll be helpful for at least some of you!我希望它至少对你们中的一些人有所帮助! Let me know what you think, there is of course a ton of work ahead to make it fully functional, but the first beta is already beneficial for basic SwiftUI usage.让我知道您的想法,当然还有很多工作要做才能使其完全发挥作用,但第一个测试版已经对基本的SwiftUI使用有益。

See demo videos of QDesigner and learn more by visiting website:查看QDesigner演示视频并访问网站了解更多信息:
https://Q-Mobile.IT/Q-Designer https://Q-Mobile.IT/Q-Designer

QDesigner1

QDesigner2

QDesigner3

For people who wanted to move preview it to the right side... Change Layout (right corner of workspace second last option) to "Canvas on Right"对于想要将预览移动到右侧的人...将布局(工作区的右上角倒数第二个选项)更改为“右侧的画布” 更改 Xcode 预览位置 giridharan

ran into the same problem, everything was up to date.遇到同样的问题,一切都是最新的。 Got it to work by turning off code coverage for all targets:)通过关闭所有目标的代码覆盖率让它工作:)

Go to editor on Xcode, and under it click Editor & Canvas.转到 Xcode 上的编辑器,然后在其下单击编辑器和画布。

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

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