繁体   English   中英

Swift ui macOS 事件检测更改强调色

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM