animation(nil) replacement in SwiftUI 3.0 (iOS 15)

Prior to iOS 15, setting .animation(nil) removed all animations which would occur to the view. However, animation(_:) was depracated in iOS 15. What is the replacement for this now? Thanks in advance.

Accepted Reply

The doc of animation(_:) says

Deprecated

Use withAnimation or animation(_:value:) instead.

Have you tried?

  • Yes I am aware, however I use withAnimation to update views that depend on it but I want some views to not have any animations. I could use animation(_:value:) (like this: animation(nil, value: state) ) however it just seems longer, as I want to disable all animations for the view not just for a specific value. However, looking over the documentation, I don't think there is a modifier that would replace the functionality of animation(nil).

Add a Comment

Replies

The doc of animation(_:) says

Deprecated

Use withAnimation or animation(_:value:) instead.

Have you tried?

  • Yes I am aware, however I use withAnimation to update views that depend on it but I want some views to not have any animations. I could use animation(_:value:) (like this: animation(nil, value: state) ) however it just seems longer, as I want to disable all animations for the view not just for a specific value. However, looking over the documentation, I don't think there is a modifier that would replace the functionality of animation(nil).

Add a Comment

You can use something like this as a general replacement to avoid figuring out a value:

extension View {
	func withoutAnimation() -> some View {
		self.animation(nil, value: UUID())
	}
}
  • Thank you, tim_at_lingq. This was the only thing that worked for me! If I put anything else in for the value: parameter (0, nil, or any variable), my view would still animate. This is the only solution that turned the animation off.

Add a Comment

I found this on SwiftLee's website that seemed to be the most compact solution. Just use in place of .animation(nil) :

.transaction { transaction in
    transaction.animation = nil
}

Happy coding!