![](/img/trans.png)
[英]SwiftUI: macOS Can't change TabView's tabItem accent color
[英]Swift ui macOS event detect change accent color
一种可能的方法是通过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
}
}
}
使用 Xcode 15 / macOS 11.5 测试
这是您可以使用的一种可能更灵活的方法,例如,对于您的视图 model:
import Combine
class SomeClass {
var cancellable: AnyCancellable?
init() {
cancellable = NSApp.publisher(for: \.effectiveAppearance).sink { appearance in
print(appearance.name.rawValue)
}
}
}
您可能必须确保调用cancellable?.cancel()
以便可以取消初始化object。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.