Post

Replies

Boosts

Views

Activity

iOS widget previews with different size family
While working through Part II the Widget code-along from WWDC, I thought I would add a separate preview for each size, just as an exercise for myself.     static var previews: some View {         Group {             EmojiRangerWidgetEntryView(entry: SimpleEntry(date: Date(), configuration: ConfigurationIntent(), character: .panda))                 .previewContext(WidgetPreviewContext(family: .systemSmall))             EmojiRangerWidgetEntryView(entry: SimpleEntry(date: Date(), configuration: ConfigurationIntent(), character: .panda))                .previewContext(WidgetPreviewContext(family: .systemMedium))         }     } } This works, giving me two previews of the correct size. Unfortunately, the smaller preview is trying to layout the medium's view within its given space. I'm assuming that this is because the @Environment for the widgetFamily wasn't configured correctly, so I thought I'd add it like this: .environment(\.widgetFamily, .systemSmall) I was guessing that something like this might be possible, because I have seen a similar situation where a preview is created for a dark mode like this: .environment(\.colorScheme, .dark) Alas, my attempt to set the widgetFamily environment doesn't work. It gives me a this error: Key path value type 'WritableKeyPath<EnvironmentValues, WidgetFamily>' cannot be converted to contextual type 'KeyPath<EnvironmentValues, WidgetFamily>' Am I close? Is there some way to inject the widget family environment, or is there a bug that fails to bind the WidgetPreviewContext(family: .systemSmall) that I'm already passing?
10
0
5.3k
Jul ’20