You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SwiftUI macOS搜索建议未遵循preferredColorScheme问题解决

解决搜索建议不跟随自定义配色方案的问题

当你把.preferredColorScheme(.light)添加到ContentView上时,只有视图层级内的元素会遵循这个设置,但搜索建议弹窗属于窗口场景(WindowScene)层级,不受ContentView的配色设置影响。

只需要把配色方案的设置移到WindowGroup层级下,就能让整个窗口包括搜索建议都强制使用浅色模式:

var body: some Scene {
    WindowGroup {
        ContentView()
        .toolbar {
            ToolbarItemGroup(placement: .navigation) {
                Button(action: {
                }, label: {
                    Image(systemName: "chevron.left")
                })
                Button(action: {
                }, label: {
                    Image(systemName: "chevron.right")
                })
            }
        }
        .searchable(text: $searchText, placement: .toolbar) {
            Text("Font family A").searchCompletion("Hello")
            Text("Font family B").searchCompletion("HelloX")
        }
    }
    .preferredColorScheme(.light) // 移到WindowGroup层级
    .windowToolbarStyle(.unified(showsTitle: false))
}

这样修改后,整个窗口的所有系统控件(包括搜索建议弹窗)都会遵循你设置的浅色模式,不再受系统全局深色模式的影响。

内容的提问来源于stack exchange,提问作者Peter Lapisu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 16:24:32