Reading time: 6 min
Pull to refresh is a common UI pattern, supported in UIKit
via UIRefreshControl
. (Un)surprisingly, it's also unavailable in SwiftUI. (EDIT: It's been added in SwiftUI 3 - however, it has many drawbacks compared to this solution.)
This recipe shows how to implement this functionality with any ScrollView
. It also doesn't rely on UIViewRepresentable
.
ScrollViewReader + onChange = Responsive Tabs
Reading time: 2 min
This recipe shows how to combine ScrollViewReader
and onChange(of:perform:)
to jump to the selected item in a scrollable view. We'll use this method to update our top tabs and make them fully visible when selected.
Flow Layout / Tag Cloud in SwiftUI
Reading time: 2 min
A Flow Layout is a container that orders its views sequentially, breaking into a new "line" according to the available width of the screen. You can compare it to a left-aligned block of text, where every word is a View
. A common use for this layout is to create a tag cloud.
Scrolling ScrollView programatically
Reading time: 1 min
This recipe is about how to scroll a SwiftUI ScrollView
programatically. Here's a quick breakdown of what to do:
- For iOS 13 - tough luck - it can't be done. For whatever reason, this is one of things that were deemed less important for the initial release of SwiftUI.
- For iOS 14 and above - embed your
ScrollView
's root view in aScrollViewReader
, and then use thescrollTo
method.