0

When we want to set the text of a UIButton, we need to execute the below code:

myButton.setTitle("My Text", forState: .normal)

The only problem is, when the text changes, an animation happens like this:

enter image description here

Is there a way to set the title of a button without it animating?

I would like the button to act as a standard UILabel, however I would also like it to call a function when it is pressed on.

iProgram
  • 6,057
  • 9
  • 39
  • 80

2 Answers2

9

You can set values inside UIView's performWithoutAnimation and it will do just as it states. But you will also need to call layoutIfNeeded so they relayout happens during the block.

UIView.performWithoutAnimation {
  self.myButton.setTitle(newTitle, forState: .Normal)
  self.myButton.layoutIfNeeded()
}
Martin Calvert
  • 1,705
  • 2
  • 11
  • 13
user6788419
  • 7,196
  • 1
  • 16
  • 28
0

Use myButton.setTitle("My Text", forState: .normal) for all Uibuttoncontrolstate.

An Kit
  • 308
  • 1
  • 2
  • 9
  • That is the code I am already using – iProgram Jan 06 '17 at 16:09
  • Have you tried same title for all UIControlState ? Normal, highlighted , selected. When we click on button it shows selected state title. When we set same title, it will not animate. – An Kit Jan 06 '17 at 16:15