How can I detect the StandBy mode on iOS 17?
When my widget is used in StandBy mode, the background is removed thanks to this method:
.containerBackground(for: .widget) {
myBackgroundImage()
}
This is very useful when my small widget is displayed on the iPad Lockscreen in landscape mode for example.
But to delimit my widget frame on the iPad lockscreen I set up an alternative light background this way:
if widgetRenderingMode == .vibrant {
Color.white.opacity(0.1).cornerRadius(cornerRadius)
}
My question: How to add the same alternative background when the widget is used in StandBy mode?
In StandBy mode, widgetRenderingMode is equal to .fullColor like on the HomeScreen. But I want to be able to add this background only in standBy mode...
Precision: containerBackground(for: .widget){}
send it's content to the background of the container so I can't hide my light background behind it as it will always be in front wherever I put it...
OK! I have found solution by myself using the showsWidgetContainerBackground
Environment Value.
I don't detect directly the StandBy mode but I detect that the WidgetContainerBackground has been removed. In widgetRenderingMode fullColor that means its the StandBy mode. At least it's true currently in iOS 17.0
@Environment(\.showsWidgetContainerBackground) var showsWidgetContainerBackground
@Environment(\.widgetRenderingMode) var widgetRenderingMode
...
ZStack {
// If ContainerBackground will be removed, show the light background
if (!showsWidgetContainerBackground && widgetRenderingMode == .fullColor) {
Color.white.opacity(0.1).cornerRadius(cornerRadius)
}
WidgetContentView()
.containerBackground(for: .widget) {
backgroundView(viewSize: panelSize)
}
}