How to prevent button from animating when clicked

Viewed 384

I'm trying to prevent a button from animating when clicked, but I have not been successful.

I've tried setting UIButton.appearance().isHighlighted, .adjustsImageWhenHighlighted and .showsTouchWhenHiglighted, to false. I've also tried:

Button(action: {}) {
   Text("X")
}
.animation(.none)
3 Answers

I think the purest solution to this problem will be not using Button. Instead you can do it like this:

Text("X")
  .onTapGesture {
    print("clicked!")
  }

Almost, to disable animation in some place we should set nil, like

Button(action: {}) {
   Text("X")
}
.animation(nil)     // << here !!

Tested with Xcode 12.1 / iOS 14.1

If you can't get it to work any other way you can disable the button and attach a tap gesture recognizer to it.

Related