[英]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.