Widgets: Detect StandBy night mode iOS 17

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...

Answered by Clem23 in 761514022

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)
          }
}
Accepted Answer

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)
          }
}

@Clem23
Did u resolved StandBy mode Background color change

There is a specific environment variable to detect StandBy mode called isActivityFullscreen.

@Environment(\.isActivityFullscreen) var isStandByMode

Is there a solution here to detect if Widget is displayed on Mac via iPhone?

I want to have a button in my interactive widget to update Health data but this is no good on the Mac because generally the user's device will be locked and therefore the HealthStore will be unavailable.

Widgets: Detect StandBy night mode iOS 17
 
 
Q