如何修复多个 SwiftUI 预览 [英] How to fix multiple SwiftUI previews
问题描述
我正在使用 SwiftUI 并编写了以下示例来说明我遇到的问题.当我添加多个按钮或多个文本时,它会创建两个单独的预览,但是当我在设备上运行应用程序时,它们会同时加载.附上一张照片:
I am working with SwiftUI and wrote the following example to present an issue I'm experiencing. 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:
我清理了我的构建文件夹,重新启动了我的计算机,并在其他各种 SwiftUI 文件上尝试了这个,但没有成功.有什么想法吗?
I cleaned my build folder, restarted my computer, and tried this on various other SwiftUI files but no success. Any ideas?
推荐答案
您需要将两个 Text
放在 VStack
或类似的地方.
You'll need to put both Text
s in a VStack
or similar.
struct ContentView: View {
var body: some View {
VStack {
Text("Example title")
Text("Example title 2")
}
}
}
似乎 Xcode 会自动为 body
变量中返回的每个单独的视图添加一个新的预览(它曾经抛出错误,但我认为 Xcode 12 改变了一些东西).
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).
这篇关于如何修复多个 SwiftUI 预览的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!