簡體   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