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
相关产品推荐
相关产品推荐

