Understanding SwiftUI's ViewModifiers and ViewBuilders

It has been more than a year since the release of SwiftUI, and we certainly had some interesting experiences with it so far. It can create a lot of  excitement, surprise, and sometimes a ton of annoyance too. If you have started making apps with SwiftUI, you might have encountered situations where if you used UIKit, the work would be done easily, but with SwiftUI it can sometimes take all day – even more. But since SwiftUI is still a baby, these things shouldn't stop one from learning and unwrapping its mysteries because, with time, it will grow, get better and make our app development way easier.

In this article, we are going to learn two of the key components of SwiftUI, which are ViewModifiers and ViewBuilders. ViewModifiers play a significant role in SwiftUI. Most of the functions called on a SwiftUI View are view modifiers. It is the primary way of modifying a view in SwiftUI. As for ViewBuilders, it is mainly used to create custom container views, which can also become a reusable view component.

In this post, we will take a look at some ready-to-use modifiers, then we will build our own custom view modifier and create a reusable custom container view with ViewBuilder. Using this we can implement gorgeous design systems that are consistent across the entire app and encourage code reusability and good architectural patterns.

Read more