0

I'm building a SwiftUI app for iPad, using the new Sidebar UI introduced with iPadOS 14. Each sidebar item should have a label accessory. Here's how I'm building it, and the corresponding result:

struct ContentView: View {

    enum NavigationItem {
        case companies, aapl
    }

    @State private var selection: NavigationItem? = .companies

    var sidebar: some View {
        List(selection: $selection) {
            NavigationLink(destination: Text("Companies"), tag: NavigationItem.companies, selection: $selection) {
                HStack {
                    Label("Companies", systemImage: "list.bullet")
                    Spacer()
                    Text("6")
                        .foregroundColor(.secondary)
                        .padding(.trailing, 6)
                }
            }
            .tag(NavigationItem.companies)

            NavigationLink(destination: Text("Apple Inc"), tag: NavigationItem.aapl, selection: $selection) {
                Label("Apple Inc", systemImage: "circle")
            }
            .tag(NavigationItem.aapl)
        }
        .listStyle(SidebarListStyle())
        .navigationTitle("Menu")
    }

    var body: some View {
        NavigationView {
            sidebar

            Text("Select an item")
                .foregroundColor(.secondary)
        }
    }
}

enter image description here

It works, but the color of the accessory label doesn't change when the item is selected. The label built with Label works as expected though.

Is this the correct way to add a label accessory to a sidebar item in SwiftUI, or is there a better way?

Marcos Tanaka
  • 3,112
  • 3
  • 25
  • 41
  • You gave explicit color for it `.foregroundColor(.secondary)`, try to remove that line. – Asperi Feb 23 '21 at 16:32
  • When I remove the explicit color it changes to white when selected, and uses `.primary` otherwise. But I would like the standard behavior, which is a slightly lighter shade of gray for the accessory label. – Marcos Tanaka Feb 23 '21 at 17:41

0 Answers0