小编Arj*_*un 的帖子

如何从应用程序更新 iOS 14 Widget 背景色?

对于我的小部件,我希望用户能够选择他们选择的小部件背景颜色,但我似乎无法让它工作。

我正在尝试用一个ColorPicker@AppStorage

ColorPicker("Background Color", selection: Binding(get: {
                    bgColor
                }, set: { newValue in
                    backgroundColor = self.updateColorInAppStorage(color: newValue)
                    bgColor = newValue
                }))
                .frame(width: 200, height: 50)
                .font(.headline)
Run Code Online (Sandbox Code Playgroud)

backgroundColor 是一个@AppStorage保存 RGB 值的变量

在我的 Widget 扩展类中,我使用这个变量在 @main 结构中设置小部件背景颜色:

@main
struct MyWidget: Widget {
    @AppStorage("backgroundColor", store: UserDefaults(suiteName: "group.com.MyWidget")) var backgroundColor = ""
    @State private var bgColor = Color(UIColor.systemBackground)
    let kind: String = "Count"

    var body: some WidgetConfiguration {
        IntentConfiguration(kind: kind, intent: ConfigurationIntent.self, provider: Provider()) { entry in
            SubscriberCountEntryView(entry: entry) …
Run Code Online (Sandbox Code Playgroud)

ios swift widgetkit swiftui

3
推荐指数
1
解决办法
1406
查看次数

标签 统计

ios ×1

swift ×1

swiftui ×1

widgetkit ×1