swiftuiwidgetkitxcode15ios17

Widget on iOS 17 Beta device - Adopt containerBackground API


I'm working on an app + widget with Xcode 15 Beta. The widget works as expected on iOS 17 Beta simulators, but when running on my device with iOS 17 beta, it displays "Please adopt containerBackground API" instead of the desired content.

enter image description here

I searched around, and could only find containerBackground on LocationButton, but I'm not using Core Location at all. Can anybody point out why it is referred here?


Solution

  • The modifier in question is .containerBackground(_:for:), which is new in iOS 17 and related platform versions.

    With it you can specify the background for your entire widget as a modifier to your custom view, e.g.:

    VStack {
      Text("My widget")
        .foregroundStyle(.secondary)
    }
    .containerBackground(.red.gradient, for: .widget)
    

    Note that in some places, e.g., if your widget displays in the new StandBy mode, the container background may not display at all.

    You can customise this behaviour by adding the modifier containerBackgroundRemovable(false) – although that may prevent your widget from being shown at all in some contexts.

    A preferable option is to subscribe to the environment variable \.showsWidgetContainerBackground to tweak your widget if you're in a context where the background is not showing.

    More details can be found in the WWDC video Bringing widgets to new places.