Home > OS >  Highlight Navigation View At Start
Highlight Navigation View At Start

Time:12-12

When I start my app, the start page is "Kunde" but the whole thing is not highlighted in blue in the navigation. It just turns blue (system color) when I click on it.

I want it to be highlighted blue when I open the app.

enter image description here

enter image description here

import SwiftUI

struct ContentView: View {
    var body: some View {
        NavigationView {
            List {
                NavigationLink(destination: ListView()) {
                    Text("Kunde")
                }
            }
            ListView()
        }
    }
}

struct ListView: View {
    var body: some View {
        Text("Hello.")
    }
}

CodePudding user response:

you could try something like this approach:

struct ContentView: View {
    @State var selection: String?
    @State var listData = ["Kunde", "xxxx", "zzzz"]
    
    var body: some View {
        NavigationView {
            List(listData, id: \.self) { item in
                NavigationLink(destination: ListView()) {
                    Text(item)
                }
                .listRowBackground(selection == item ? Color.blue : Color.clear)
            }
        }
        .onAppear {
            selection = "Kunde"
        }
    }
}
  • Related