How can i get animation when click a button that in List?

103 views Asked by At

My code is like this:

       List {
            Button(action: {}, label: {
                Text("Button")
            })
            Button(action: {}, label: {
                Text("Button")
            })
        }

when click button, it has none animation, it's not change the opacity, this not change backgroud color. But when press button, the backgroud color is changed. this result is: enter image description here

The effect I except is click button and backgroud color changed immediately, like this: enter image description here

I try to set buttonStyle by using code

struct clickButton: ButtonStyle {
    func makeBody(configuration: Configuration) -> some View {
        configuration.label
            .background(configuration.isPressed ? Color.gray : Color.white)
    }
}

but the result is terrible.

1

There are 1 answers

0
Mojtaba Hosseini On BEST ANSWER

The second image you provided is the default behavior of the NavigationLink:

var body: some View {
    NavigationView {
        List {
            NavigationLink("Button") {
                Text("Next")
            }
        }
    }
}

And the issue you reported is because the buttonStyle is automatic when you use it in a list. You can change it to anything but the automatic. like:

List {
    Button(action: {}, label: {
        Text("Button")
    })
}
.buttonStyle(.borderless) //  Here