Last week, I linked to an article by John Sundell about Observing the content offset of a SwiftUI ScrollView, so you might imagine my surprise when I saw yet another article about the very same topic.
While both articles cover the same topic, they do take different approaches. John exposes the
ScrollView's content offset via a
Binding, whereas Daniel chose to use an action (
onScroll). His reasoning:
I find using a binding confusing when it’s not possible to change the value to scroll to a certain offset. Since the binding is thus in fact read-only and writing to it would lead to bugs, I decided use an action.
This makes a lot of sense to me, and I think it would be a great idea to compile a list of SwiftUI design patterns like this.
As an added bonus, Daniel released a Swift package which allows implementing sticky header views and other scrolling-related effects: ScrollKit
I think this is the first time I link to two articles by the same author in the same week, but it's totally worth it. In this one, Daniel describes how to replicate the stretchy header known from apps like Spotify. Conveniently, this is part of ScrollKit, so you can easily integrate it your own apps.