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)
}
}
}
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?