SwiftUI + macOS + Xcode : Show list item in navigation pane as selected programatically at program start


I have a SwiftUI program that displays one of three views based on a selection from a list in a Navigation Pane. I am able to display an initial view programatically. At this point there is a border around the initialized selection in the Navigation Pane but it is dark grey. Once selected with the mouse the border turns blue and now I can move up and down the list with the arrow keys on the keyboard. Is there a way to programatically perform what ever the mouse is doing to change the background color to blue and thus allow me to move up and down the list with the arrow keys?

Below is the code.



func fetchView(item : Int) -> some View {

switch item {
case 1: FirstView()
case 2: SecondView()
case 3: ThirdView()


struct ContentView: View {

@State private var initialView: Int? = 1
var theViewNames : [String] = ["First View","Second View", "Third View"]

var body: some View {
        NavigationView {
                Text ("Select View").bold()
                    .frame(height: 20)
                List (1..<4) {item in
                    NavigationLink (destination: fetchView(item: item), tag: item, selection: self.$initialView ) {Text (theViewNames[item - 1])}
    }.frame(width: 900, height: 600, alignment: .center)


Terms of Service

Privacy Policy

Cookie Policy