对于我的小部件,我希望用户能够选择他们选择的小部件背景颜色,但我似乎无法让它工作。
我正在尝试用一个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)