Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/107.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Can';t更改iOS14小部件的背景颜色_Ios_Widget_Swiftui_Background Color - Fatal编程技术网

Can';t更改iOS14小部件的背景颜色

Can';t更改iOS14小部件的背景颜色,ios,widget,swiftui,background-color,Ios,Widget,Swiftui,Background Color,我知道我可以使用以下代码更改SwiftUI中的视图背景颜色: .background(Color(.systemGroupedBackground)) struct XWidget: Widget { // MARK: Widget is defined here var body: some WidgetConfiguration { StaticConfiguration( kind: "xWidget",

我知道我可以使用以下代码更改SwiftUI中的视图背景颜色:

.background(Color(.systemGroupedBackground))
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])
    }
}
但我不能为小部件的背景色本身做这件事

我使用以下代码:

.background(Color(.systemGroupedBackground))
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])
    }
}
structxwidget:Widget{//MARK:Widget在这里定义
变量主体:一些WidgetConfiguration{
静态配置(
种类:“xWidget”,
提供程序:xProvider(),
占位符:文本(“加载…”){中的条目

xWidgetEntryView(条目:entry).background(颜色(.systemGroupedBackground))//您需要指定完整帧,如下演示所示

静态配置(
种类:“xWidget”,
提供程序:xProvider(),
占位符:文本(“加载…”){中的条目
xWidgetEntryView(条目:条目)

.frame(maxWidth:.infinity,maxHeight:.infinity)//如果要更改小部件的背景色,应修改Assets.xAssets

小部件目标中的Assets.xcsets有一个名为“WidgetBackground”的颜色集

更改“WidgetBackground”颜色不仅会更改小部件的背景色,还会更改小部件库中显示的小部件的add按钮的背景色


您还可以使用ZStack在小部件视图中设置颜色,如下所示:

var body: some View {
        
       VStack {
            ZStack {
                Color.black
                    .ignoresSafeArea()
                Link(destination: Deeplink.image.url!) {
                    Image("exampleImage")
                        .resizable()
                        .aspectRatio(contentMode: .fit)
                        .padding(.vertical, 3)
                }
            }
            Text("Example text")
        }
    }
对于那些试图更改支持模式的小部件背景的用户

更改小部件背景颜色(支持暗模式)
  • 转到小部件扩展中的
    Assets.xcsets
  • 应该已经有一个名为“WidgetBackground”的颜色集
  • 如果缺少,则创建一个新的颜色集,并将其命名为
    “WidgetBackground”
  • 转到属性检查器,确保外观设置为
    “任何,黑暗”
  • 转到您的小部件扩展构建设置并搜索资产目录编译器-选项
  • 确保小部件背景颜色名称设置为颜色集“WidgetBackground”的名称
  • 转到小部件视图并设置其背景颜色
    。背景(颜色(“WidgetBackground”)
  • 公共变量主体:一些WidgetConfiguration{
    静态配置(种类:种类,提供程序:MyCustomTimeline()){中的条目
    MyCustomWidgetView(条目:条目)
    .背景(颜色(“WidgetBackground”))
    }
    }
    
  • 编译并在设备(或模拟器)上运行,并检查当前外观的颜色更新

  • 这对我不起作用。它更改了按钮的颜色,但小部件的颜色仍然相同。您是否可以演示如何为夜间和白天设置2种颜色?这在右侧的属性检查器中;您可以选择外观(任意、亮、暗)不起作用…如何设置画布颜色?这似乎不起作用。由于某种原因,生成失败,如果设置为“无”以外的任何值,则无法找到最新可用的模拟器运行时谢谢你的工作。但是我如何定义白天和黑暗模式的颜色?你能告诉我如何设置图像而不是颜色吗?我使用这个和WidgetBackround颜色资产的组合,在定义了光明和黑色的WIdgetBackground颜色后,在上面的代码中使用color(“WIdgetBackground”)而不是color.green。