简体   繁体   English

选择导航链接时如何更改文本颜色? SwiftUI Mac催化剂

[英]How do I change text color when navigation link is selected? SwiftUI Mac Catalyst

I have used a List of NavigationLink to generate my sidebar but when running as a Mac Catalyst app all selections use the system accent color as their background.我使用了NavigationLink List来生成侧边栏,但是当作为 Mac Catalyst 应用程序运行时,所有选择都使用系统强调色作为背景。 This is fine but where the colour is dark I want the text to invert to white like it does in most Mac apps.这很好,但是在颜色较深的地方,我希望文本像大多数 Mac 应用程序一样反转为白色。

Can anyone help?任何人都可以帮忙吗? Here's my code:这是我的代码:

 List {
            
            ForEach(topics, id: \.self) { topic in
                
                NavigationLink(
                    destination: DetailView(selectedDate: self.titles[topic])
                ) {
                    HStack {
                        Image(topic)
                            Text(self.titles[topic]!)
                }
                 
                }
            
            }
           
            
    }

Btw topic is an array of strings which are the keys for the dictionary titles .顺便说一句, topic是一个字符串数组,它们是字典titles的键。 Thanks in advance.提前致谢。

侧边栏

Have you tried using the new Label(_:) element, instead of a custom HStack with image + text?您是否尝试过使用新的Label(_:)元素,而不是带有图像 + 文本的自定义 HStack? Swift "2" now has Label which when coupled with the SidebarListStyle should do what you want to. Swift “2” 现在有LabelSidebarListStyle结合时应该可以做你想做的事。 Text goes to highlighted color when selected.选中后,文本变为突出显示的颜色。 Since you're using custom images, I'm not sure what happens there, but give it a try.由于您使用的是自定义图像,我不确定那里会发生什么,但试一试。

Instead of:代替:

HStack {
  Image(...)
  Text(...)
}

Just use:只需使用:

Label("Formalism", image: "formalism.png")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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