macOS SwiftUI 问答之 Swift ui macOS 事件检测更改
2023-09-11 14:18:31 时间
实战需求
在一般设置中是否有确定强调色类型更改的事件?
解放方案
一种可能的方法是NSUserDefaults通过AppStorage观察者使用更改,例如
struct ContentView: View {
@AppStorage("AppleAccentColor") var appleAccentColor: Int = 0
var body: some View {
Text("Hello world!")
.foregroundColor(.accentColor) // << updated automatically
.onChange(of: appleAccentColor) { _ in
print("Side-effect is here")
// also can be read via NSColor.controlAccentColor
}
}
}
加入我们一起学习SwiftUI
QQ:3365059189
SwiftUI技术交流QQ群:518696470
教程网站:www.openswiftui.com