Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix multiple SwiftUI previews

Tags:

xcode

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:

enter image description here

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

like image 729
EJZ Avatar asked Dec 31 '22 18:12

EJZ


1 Answers

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

like image 85
aheze Avatar answered Jan 18 '23 15:01

aheze