Post

Need to toggle between iconOnly and titleAndIcon label styles in SwiftUI? You can create your own custom LabelStyle and add some fun transition animations :D code below 👇

The custom label style which takes in a isExpanded boolean and transitions with a matched geometry effect alongside an opacity and scale transition. You can customize this any way you want!

And here is the view which calls this custom label style:

0 reposts1 like

Thanks for sharing! You might even not need the matchedGeometryEffect. Unfortunately I wasn't able to pass a generic transition to make the toggle effect completely customised (or only provide an AnyTransition which does not include all transitions like blurReplace). cc @matt1corey.bsky.social

It comes down to a faulty assumption on my part - that a Label _should_ be able to smoothly adapt from one built-in style to another. Clearly, this isn't true. What bugs me is that I don't see a reason this assumption _shouldn't_ be true, but rather, I think it's simply a shortcoming of the API. 🧵

... and that brings me back to my original comment - that there are a bunch of these minefields in SwiftUI that we need to deal with, presumably because of a simple poor decision in the early API design. A well-behaved 'if' modifier would be a godsend to deal with these better!

One thing that boggles me is why there are both the AnyTransition Struct type & the Transition Protocol. Yet I don't fully understand how to use the static methods on the Transition Protocol. BlurReplaceTransition isn't an AnyTransition so we can't place it in a ternary operator for a transition.