whats the difference between ScrollViewReader and ScrollProxy
Asked on 2024-08-02
1 search
The difference between ScrollViewReader and ScrollProxy in SwiftUI is primarily about their roles and how they are used to control scroll views.
-
ScrollViewReader:
ScrollViewReaderis a view that provides aScrollProxyto its content. It allows you to programmatically control the scroll position of aScrollView.- You wrap your
ScrollViewinside aScrollViewReaderand use theScrollProxyprovided by theScrollViewReaderto scroll to specific positions or items. - Example usage:
ScrollViewReader { proxy in ScrollView { ForEach(0..<100) { index in Text("Item \(index)") .id(index) // Assign an ID to each item } } .onAppear { proxy.scrollTo(50) // Scroll to item with ID 50 } }
-
ScrollProxy:
ScrollProxyis the object provided byScrollViewReaderthat you use to perform the actual scrolling actions.- It provides methods like
scrollTo(_:anchor:)to scroll to a specific view identified by an ID. - The
ScrollProxyis what gives you the programmatic control over the scroll view's content.
In summary, ScrollViewReader is the container that provides a ScrollProxy, and ScrollProxy is the tool you use to control the scroll position within the ScrollView.
For more detailed information on scroll view enhancements in SwiftUI, you can refer to the session What’s new in SwiftUI.

What’s new in SwiftUI
Learn how you can use SwiftUI to build great apps for any Apple platform. Explore a fresh new look and feel for tabs and documents on iPadOS. Improve your window management with new windowing APIs, and gain more control over immersive spaces and volumes in your visionOS apps. We’ll also take you through other exciting refinements that help you make expressive charts, customize and layout text, and so much more.

Create custom visual effects with SwiftUI
Discover how to create stunning visual effects in SwiftUI. Learn to build unique scroll effects, rich color treatments, and custom transitions. We’ll also explore advanced graphic effects using Metal shaders and custom text rendering.

Enhance your UI animations and transitions
Explore how to adopt the zoom transition in navigation and presentations to increase the sense of continuity in your app, and learn how to animate UIKit views with SwiftUI animations to make it easier to build animations that feel continuous.
