How to remove opacity animation in SwiftUI NavigationLink
Asked Answered
P

1

8

When tapping on a NavigationLink, it reduces the opacity slightly. Is there a way to disable this. I tried using .buttonStyle(PlainButtonStyle()) but that didn't have the desired effect.

It is embedded in a scrollView (preferred over List for customizability):

ScrollView {
    ForEach(items){ item in
        NavigationLink(destination: DetailView()){
            HStack{
                Text("title")
                Spacer()
                Image(systemName: "chevron.right")
            }
            .padding()
            .background(
                RoundedRectangle(cornerRadius: 10, style: continuous)
                    .foregroundColor(Color.gray)
            )
        }
    }
}
Preemption answered 10/6, 2020 at 17:30 Comment(2)
You mena opactiy of the Button?Sudan
@Sudan Not exactly. Just the opacity animation that occurs when a Navigation Link is tapped.Preemption
D
46

Here is possible solution. Tested with Xcode 11.4 / iOS 13.4

Use custom button style that just returns label view (w/o highlight effect)

struct FlatLinkStyle: ButtonStyle {
    func makeBody(configuration: Configuration) -> some View {
        configuration.label
    }
}

and

    NavigationLink(destination: DetailView()){
        HStack{
            Text("title")
            Spacer()
            Image(systemName: "chevron.right")
        }
        .padding()
    }.buttonStyle(FlatLinkStyle())     // << here !!
Drowsy answered 10/6, 2020 at 18:55 Comment(1)
works in Xcode 12 and iOS 14.2 also. ThanksKnee

© 2022 - 2025 — McMap. All rights reserved.