简体   繁体   English

如何修复多个 SwiftUI 预览

[英]How to fix multiple SwiftUI previews

I am working with SwiftUI and wrote the following example to present an issue I'm experiencing.我正在使用 SwiftUI 并编写以下示例来展示我遇到的问题。 When I add multiple Buttons, or multiple Texts, it creates two separate previews, but when I run the application on a device they load simultaneously.当我添加多个按钮或多个文本时,它会创建两个单独的预览,但是当我在设备上运行应用程序时,它们会同时加载。 Attached here is a photo:附上一张照片:

在此处输入图像描述

I cleaned my build folder, restarted my computer, and tried this on various other SwiftUI files but no success.我清理了构建文件夹,重新启动了计算机,并在其他各种 SwiftUI 文件上进行了尝试,但没有成功。 Any ideas?有任何想法吗?

You'll need to put both Text s in a VStack or similar.您需要将两个Text都放在VStack或类似文件中。

struct ContentView: View {
    var body: some View {
        VStack {
            Text("Example title")
            Text("Example title 2")
        }
    }
}

It seems that Xcode automatically adds a new preview for each separate view returned in the body var (It used to throw an error, but I think Xcode 12 changed some things).似乎 Xcode 会自动为body var 中返回的每个单独视图添加一个新的预览(它曾经抛出错误,但我认为 Xcode 12 改变了一些东西)。

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

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