preferredColorScheme does not work with previewContext

I can't seem to get xcode canvas preview to display my widget under different color schemes.

Pretty simple to replicate:

  • Create new app
  • Create widget extension
  • Preview widget under different color schemes (see code below)
struct BugWidget_Previews: PreviewProvider {
  static var previews: some View {
    BugWidgetEntryView(entry: SimpleEntry(date: Date(), configuration: ConfigurationIntent()))
      .previewContext(WidgetPreviewContext(family: .systemSmall))
      .preferredColorScheme(.light)
    BugWidgetEntryView(entry: SimpleEntry(date: Date(), configuration: ConfigurationIntent()))
      .previewContext(WidgetPreviewContext(family: .systemSmall))
      .preferredColorScheme(.dark)
      
  }
}

I have tried changing the order of modifiers, and even selecting dark mode through the canvas appearance editor menu.

When run on the simulator the UI responds as would be expected to dark mode.

Hi,

Sorry to hear you are having trouble getting the color scheme to apply to the Widget previews. Worth filing a bug with a simple sample project.

As a workaround could you try using Xcode 14's preview variants support?

preferredColorScheme does not work with previewContext
 
 
Q