Search code examples
xcodeswiftuipreviewxcode14

XCode Preview with Color Loop for SwiftUI Previews not working anymore


Before XCode 14, the following syntax was working well to loop through the different color schemes.

static var previews: some View {
    
    ForEach(ColorScheme.allCases, id: \.self) {
        MyView()
            .preferredColorScheme($0)
    }
}

Starting XCode 14, this results in

PreviewUpdateTimedOutError: Updating took more than 5 seconds

I suspect, this has to do with the new button in XCode to display the Color Scheme Variants in Preview now, but i rather like the old behaviour back.

enter image description here

Any ideas how to do that?


Solution

  • This is an Xcode bug Apple is aware of: https://developer.apple.com/forums/thread/711723 As a workaround, you can use preview variants support as suggested there.

    If you just want to see the dark mode case (or something similar to it), this is what I've been doing:

            .environment(\.colorScheme, .dark)
            .background(Color.black)
    

    This is not exactly the same than preferredColorScheme(.dark) but it works good enough to have something in the preview while Apple fixes it.