I'm working on an iOS widget and it seems @Environment
does not work here at all. I tried using @Environment(\.widgetFamily)
but always got .systemMedium
. So far not one environment value seems to work in my widget: neither the ones provided by the system, nor custom environment values.
Example:
struct WidgetEntryView : View {
@Environment(\.widgetFamily) private
var widgetFamily
@Environment(\.controlSize) private
var controlSize
var body: some View {
Text("\(String(describing: self.widgetFamily))/\(String(describing: self.controlSize))")
}
}
@main
struct MainWidget: Widget {
let kind: String = "Widget"
var body: some WidgetConfiguration {
return IntentConfiguration(kind: kind,
intent: MyWidgetIntent.self,
provider: MyTimelineProvider())
{
(entry) in
WidgetEntryView()
.controlSize(.large)
}
.configurationDisplayName("Widget Title")
.description("Widget Description")
}
}
Using Xcode 14.0 on an iOS 15.5 device, this always prints "systemMedium/regular", no matter which widget size I use or which control size I manually set.
(I can also reproduce this with other views inside my view hierarchy, it's not just affecting the top-level view.) Is this a known problem? Is there a workaround, apart from passing the values I care about as explicit parameters/properties?