swiftuiwidgetwatchos

How to set the background of a SwiftUI accessoryCorner watch widget?


I want to setup a very simple accessoryCorner SwiftUI widget, but it displays ugly.

This is my code:

struct WidgetView: View {
    @Environment(\.widgetFamily) var widgetFamily
    var body: some View {
        switch widgetFamily {
            case .accessoryCorner:
                Image(systemName: "cart")
                    .widgetLabel {
                        Text("Label")
                    }
            default:
                Text("?")
        }
    }
}  

This yields the following watch face:
enter image description here

For some reason, the image (the cart) is displayed in white color on a nearly white background, i.e. it cannot be seen.
I tried various methods to set a better background, e.g. ZStack with AccessoryWidgetBackground(), background(Color.clear), etc., but none worked.

How to display the image without a background, like the day (DI) in the left upper corner?


Solution

  • UPDATE:

    By now, the problem is solved.
    I got a hint from Xcode 15.1 preview:
    enter image description here

    I then modified my WidgetConfiguration as follows:

    var body: some WidgetConfiguration {
        StaticConfiguration(kind: widgetKind, provider: Provider()) { entry in
            if #available(iOS 17.0, watchOSApplicationExtension 10.0, *) {
            WidgetView(entry: entry)
                .containerBackground(.fill.tertiary, for: .widget)
            } else {
                WidgetView(entry: entry)
                    .padding()
                    .background()
            }
        }
    

    and the accessory corner widget is created using

    private func accessoryCorner(entry: Provider.Entry) -> some View {
        entry.image
            .modifier(ContainerBackground())
            .font(.system(size: 20))
            .widgetLabel {
                Text"Some text")
            }
    }
    

    The widget is now displayed correctly (see top right corner):
    enter image description here

    PREVIOUS ANSWER:

    I contacted Apple and got the following answer:

    We have reviewed your request and have concluded that there is no supported way to achieve the desired functionality given the currently shipping system configurations.