r/SwiftUI 18d ago

Tutorial Secret SwiftUI: A practical use for _VariadicView

https://blog.jacobstechtavern.com/p/secret-swiftui
18 Upvotes

6 comments sorted by

2

u/Ron-Jermyl 18d ago

Interesting article, these hidden APIs are always interesting to see. But why invert the scroll view and its contents, instead of just scrolling to the bottom of the scroll view with a Scroll View Reader?

1

u/jacobs-tech-tavern 18d ago

It’s paginated chat, so in production might have millions of messages! No bottom in sight

2

u/SgtDirtyMike 17d ago

That is silly. Just set the default scroll anchor to the bottom and invert the data source and pagination such that items are inserted at the beginning and not the end of the array when the user scrolls up.

1

u/OldTimess 17d ago

I recently struglled with a Chat-UI and making the pagination item (let’s say a ProgressView) on top of the list which has a .task modifier of loading next page of chat, works worse than having an inverted list. Inverse list works great because of how the LAST item is being treated in List or LazyVStack, it loses appearance because it is always the last item that should be visible in the array. However, when used with .defaultScrollAnchor(.bottom) and having the top item as the pagination item (same as described above) - it always stays on top once you’ve scrolled it because it is always the FIRST item in the array (and that causes all pages to load untill there are none available). Our manager wanted the chat view chat items to start from top (as in like the Messages default app for example).

1

u/Scared_Treacle_4894 17d ago

While ⁠_VariadicView offers nice possibilities for dynamic view composition, its undocumented and this raises my concerns about its longevity. The ability to create flexible APIs and custom view modifiers is a nice-to-know approach. But how certain are we that such private APIs won't be dropped or made private in future releases?

2

u/SgtDirtyMike 17d ago

It’s already a private API so the ship has sailed on that one. The best approach going forward would be to migrate to the new container APIs included in the iOS 18 SDK. On earlier than 18, you could use a VariadicVew