Ivan Santos
Ivan Santos's Blog

Ivan Santos's Blog

Swift UI Preview - Nice to know

Swift UI Preview - Nice to know

Ivan Santos's photo
Ivan Santos
·Feb 13, 2022·

1 min read

As I am learning how to create iOS, I have learn a thing or two (literally two things I want to share)

Environment Variables

If you are using environment objects, you probably already faced this issue, where in the preview, it asks for the environment variable. All you gotta do is add environment object itn he PreviewProvider.

struct ListView_Previews: PreviewProvider {
    static var previews: some View {
            ListView()
                .environmentObject(ListViewModel())44
    }
}

Navigation View

If you are using Navigation View, and you are creating a new view, you will probably noticed that your navigation title does not display in the Live Preview. To fix that, all you need to do is add Navigation View in the PreviewProvider method, like so:

struct NoWorkoutView_Previews: PreviewProvider {
    static var previews: some View {
        NavigationView {
            NoWorkoutView()
                .navigationTitle("Workout")
        }
    }
}
 
Share this