Understanding ScrollView and Building a Carousel UI

Overview

This chapter draws on everything you learned how to do (and more) in the previous three, and leads you through the process of building a vertical (and later horizontal) image and text carousel.

As you dive in, there are a few things to take note of:

Key Takeaways

  • ScrollView wraps any content to make it scrollable — use ScrollView(.horizontal) for a side-scrolling carousel.
  • A CardView is a reusable struct defined in its own file; every place you call it — including PreviewProvider — must supply values for any variables it declares.
  • “Refactoring” in this chapter means extracting hardcoded content into variables so the same CardView struct can be reused with different data — a pattern you’ll use repeatedly.
  • The PreviewProvider struct calls your view struct to render a canvas preview; when the struct has parameters, the preview must pass matching values.
  • Try to complete the full tutorial before downloading the finished file — the homework assignment is structurally similar and there is no download available for it.

Read Chapter 5: Understanding ScrollView and Building a Carousel UI in Mastering SwiftUI