ViewBuilder Tutorial: Creating Reusable SwiftUI Views

Learn how to use ViewBuilder to create reusable SwiftUI views. Understand how to refactor views, including how to use type constraints and convenience initializers. By Renan Benatti Dias.

5 (6) · 2 Reviews

Download materials
Save for later
Share
You are currently viewing page 4 of 4 of this article. Click here to view the first page.

Where to Go From Here?

You can download the completed project files by clicking Download Materials at the top or bottom of the tutorial.

In this tutorial, you learned:

  • How to use a @ViewBuilder to build views inside other views.
  • Why code duplication should be avoided and how to create views to extract repeating code.
  • How to use generic types and type constraints to accept different kinds of collections in a view.

Learning when and how to build reusable views will help you maintain high quality code.

Want to dive deeper into @ViewBuilder? Check out the developer documentation.

Up for a really deep dive? Watch this video from WWDC 21 on how to use result builders to write your own domain-specific language (DSL).

Did you love working with RayGem? Look back at when it first appeared and learn about writing multiplatform apps that run across all Apple devices in Multiplatform App Tutorial: SwiftUI and Xcode 12.

To learn more about how to build apps using SwiftUI, check out our book SwiftUI Apprentice.

We hope you enjoyed this tutorial. Please join the forum discussion below if you have any questions or comments!