SwiftUI: Fix Weird Text Animation with one line of code.

--

While developing with SwiftUI, I’m pretty sure you have seen this animation. Sometimes this effect seems innocent, but the main problem with it is that you don’t have any control over it. And in most cases, this unwanted text animation seems unfit. So, I will introduce you the fix.

Source Code

Source code

I will briefly explain this code. When user clicks button, clicked flag’s value will be changed and the changes made will trigger an event to change button text. This is when the weird animation occurs.

Fix

.id(text)

Add this modifier to the view which contains text.

Fix with id(text)

Entire Code

Conclusion

  • When you apply animation on Text, it can have unwanted animation
  • Apply .id(text) if you want to get rid of the unwanted animation

--

--

KD Knowledge Diet
KD Knowledge Diet

Written by KD Knowledge Diet

Software Engineer, Mobile Developer living in Seoul. I hate people using difficult words. Why not using simple words? Keep It Simple Stupid!