SwiftUI NavigationLink shows as highlighted when going back from destination view

681 Views Asked by At

I got a NavigationLink inside a Form (screenshot 1). When I click, the navigation goes to a detail page (screenshot 2). When I go back by using either the back button or swipe gesture, the NavigationLink turns into a highlighted state (screenshot 3).

See below my code.

How should I fix this issue? Thanks.

screenshot 1 screenshot 2 screenshot 3

Home (where the first navigationlink is):

NavigationView {
                    // Content
                    VStack {
                        // Navigation to profileview
                        NavigationLink(destination: ProfileView(showing: self.$profileClicked).environmentObject(self.user),
                                       isActive: self.$profileClicked) {}
                    }
                    .navigationBarTitleDisplayMode(.inline)
                    .toolbar {
                        ToolbarItem(placement: .navigationBarTrailing) {
                            Button(action: {self.profileClicked.toggle()}) {
                                Image(systemName: "person.circle.fill")
                            }
                        }
                    }
                }

Profileview (screenshot 1):

VStack {
        
        // ...
        
        List {
            Section {
                NavigationLink(destination: AccountInfoView().environmentObject(self.user)) {
                    HStack {
                        Image(systemName: "person.fill")
                            .foregroundColor(Color("main"))
                        Text("Account information")
                    }
                }
            }
        }
        
        Spacer()
        
        // ...
    }
    .navigationTitle("Profile")
}

Accountview (2nd screenshot)

var body: some View {
    VStack {
        Form {
            HStack {
                Text("ID:")
                    .padding(8)
                Spacer()
                Text(self.user.id)
                    .foregroundColor(Color.gray)
                    .font(.system(size: 13))
                    .lineLimit(1)
            }
            HStack {
                Text("Email:")
                    .padding(8)
                Spacer()
                Text(self.user.email)
                    .foregroundColor(Color.gray)
                    .lineLimit(1)
            }
        }
    }
    .navigationTitle("Account")
}
2

There are 2 best solutions below

8
Mohmmad S On

Just add .onAppear on List and set selectionStyle to none

List{}.onAppear {
  UITableViewCell.appearance().selectionStyle = .none
}

make sure to have everything wrapped up in an NavigationView

0
pavan309 On

We observed the same issue. Unhighlighting of cells is not working if the List is added inside the VStack.

The following snippet is not working on iOS 14.

  UITableViewCell.appearance().selectionStyle = .none

The way we were able to fix it is by using LazyVStack.

We migrated from hierarchy VStack->List to VStack->ScrollView->LazyVStack->ForEach