Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I remove Button opacity animation on tap?

I hava a custom dropdown list and the top item is a button (because .onTapGesture didn't work with the items I have in the title) for which I want to remove the tap effect (oppacity change of the text). When I tap on selected Item I would like to be the same as a simple Text, no tap effect. enter image description here

I tried .animation(nil) and .buttonStyle(.plain) but with no luck.

                    Button(action: {
                        isSelecting = !isSelecting
                    }) {
                        HStack {
                            Text(selectedItem?.title ?? "Select Item")
                                .animation(.none)
                            Spacer()
                            if items.count > 1 {
                                Image(systemName: "chevron.down")
                            }
                            
                        }
                        .padding(.horizontal)
                    }
                    .buttonStyle(.plain)
                    .animation(nil)
                    .disabled(items.count < 2)
                    .onTapGesture {
                        isSelecting.toggle()
                    }
like image 301
Lce Avatar asked Dec 06 '25 07:12

Lce


1 Answers

You can create a custom ButtonStyle. In makeBody, do not check configuration.isPressed. Always return an accentColor-colored label.

struct NoHighlightButtonStyle: ButtonStyle {
    func makeBody(configuration: Configuration) -> some View {
        // handle button role if necessary
        // if configuration.role == .destructive {
        //     configuration.label.foregroundColor(.red)
        // } else {
            configuration.label.foregroundColor(.accentColor)
        // }
    }
}
like image 61
Sweeper Avatar answered Dec 08 '25 21:12

Sweeper