Better Programming

Advice for programmers.

Member-only story

16 Useful Extensions for SwiftUI

Some useful code snippets I repeatedly use in projects

Mark Lucking
Better Programming
Published in
5 min readMay 25, 2022

--

Photo by KS KYUNG on Unsplash

As we get closer to WWDC 2022 and possibly version 4.0 of SwiftUI, a framework I've been lucky to have played with almost as long as it’s been around, I thought I might publish a small collection of extensions I find myself using again and again. I hope some of these make it into the next release.

1. Hide

This is a view modifier that lets you show or hide views that you usually might not be able to. It’s worth mentioning that a view modifier is a super helpful pattern you’d do well to commit to memory.

This you use simply as a modifier on your view, the condition variable is simply a bool.

.modifier(Show(isVisible: condition))

This works well, but as you’ll notice, it does free up space on the view and forces a redraw — so a performance hit. Alternatively, you can also always use the opacity tab to get a similar effect. It will run faster but will not free up the space you used.

--

--

Mark Lucking
Mark Lucking

Written by Mark Lucking

Coding for 35+ years, enjoying using and learning Swift/iOS development. Writer @ Better Programming, @The StartUp, @Mac O’Clock, Level Up Coding & More

Responses (3)