How to fix multiple SwiftUI previews for text

谁都会走 提交于 2021-01-05 11:05:37

问题


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:

I cleaned my build folder, restarted my computer, and tried this on various other SwiftUI files but no success. Any ideas?


回答1:


You'll need to put both Texts in a VStack or similar.

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).



来源:https://stackoverflow.com/questions/65385721/how-to-fix-multiple-swiftui-previews-for-text

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!