How to remove opacity animation in SwiftUI NavigationLink

烂漫一生 提交于 2021-01-06 04:48:25

问题


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

回答1:


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 !!


来源:https://stackoverflow.com/questions/62309598/how-to-remove-opacity-animation-in-swiftui-navigationlink

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!