Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 869
Ahmadreza Avatar asked Jul 22 '20 07:07

Ahmadreza


People also ask

How do I change the background color of my widgets?

Colour and transparecy options are available in Widget Settings. If you want bright font color and dark widget background, you can check the box for «Dark background color».

Can I change app icons on iOS 14?

Not only can you change app icons with shortcuts of your own, customize app icons even more, and place widgets on the home screen, but iOS 14 also allows you to create custom widgets and welcomes support for third-party widgets for users to try out.

How to change widget background on iPhone/iPad?

Instead of having a plain widget, You can also set the background of a widget using any photo of your choice from your iPhone/iPad. The information on the widget will display over the background image. Now, that we are done with customizing our widget color, theme, and background, let’s set the widget on the home screen of our iPhone and iPad.

How to create custom fonts and themeing for color widgets in iOS?

Start by downloading/installing the Color Widgets app from the App Store You will see a lot of color options and styling for widgets Next, you can also tweak the font style and background as per your choice. To create a custom font and themeing use the Edit Widget option.

How to add widgets to iOS 14 home screen?

While Apple has finally welcomed the idea of widgets on iOS, there’s still a lot of work that needs to be done to make them more useful or at least more aesthetic to be looked at. In order to create a customized widget for your home screen on iOS 14, you need to download and install third-party apps on your iPhone.


2 Answers

For those who are trying to change the widget background that supports both dark and light modes.

Change widget background color (supports dark mode)

  1. Go to Assets.xcassets in your Widget Extension
  2. There should be a Color Set already with name "WidgetBackground"
  3. If it's missing, then create a new Color Set and name it as "WidgetBackground"
  4. Go to the Attribute Inspector and make sure the Appearances is set to "Any, Dark"

enter image description here

  1. Go to your Widget Extension's Build Settings and search for the Asset Catalog Compiler - Options
  2. Make sure the Widget Background Color Name is set to the name of the Color Set "WidgetBackground"

enter image description here

  1. Go to your widget view and set its background color .background(Color("WidgetBackground"))
public var body: some WidgetConfiguration {
    StaticConfiguration(kind: kind, provider: MyCustomTimeline()) { entry in
        MyCustomWidgetView(entry: entry)
            .background(Color("WidgetBackground"))
    }
}
  1. Compile and Run on device (or Simulator) and check the color updates to the current appearance

enter image description here enter image description here

like image 74
choofie Avatar answered Sep 22 '22 21:09

choofie


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

backup

like image 24
Asperi Avatar answered Sep 18 '22 21:09

Asperi