Search code examples
ioswidgetswiftuibackground-color

Can't change the iOS14 widget background color


I know I can change the views background colors in SwiftUI with this code:

.background(Color(.systemGroupedBackground))

But I can't do it for widget background color itself!

I use this code:

struct XWidget: Widget { // MARK: Widget is defined here
    var body: some WidgetConfiguration {
        StaticConfiguration(
            kind: "xWidget",
            provider: xProvider(),
            placeholder: Text("Loading...")) { entry in
            xWidgetEntryView(entry: entry).background(Color(.systemGroupedBackground)) // <= here
        }.supportedFamilies([.systemSmall, .systemMedium, .systemLarge])
    }
}

But the result is like this:

enter image description here


Solution

  • You need to specify full frame, as on below demo

    demo

    StaticConfiguration(
        kind: "xWidget",
        provider: xProvider(),
        placeholder: Text("Loading...")) { entry in
        xWidgetEntryView(entry: entry)
           .frame(maxWidth: .infinity, maxHeight: .infinity)    // << here !!
           .background(Color.green)
    }.supportedFamilies([.systemSmall, .systemMedium, .systemLarge])