whats the difference between ScrollViewReader and ScrollProxy
Generated on 8/2/2024
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:
ScrollViewReader
is a view that provides aScrollProxy
to its content. It allows you to programmatically control the scroll position of aScrollView
.- You wrap your
ScrollView
inside aScrollViewReader
and use theScrollProxy
provided by theScrollViewReader
to 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:
ScrollProxy
is the object provided byScrollViewReader
that 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
ScrollProxy
is 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.
Migrate your TVML app to SwiftUI
SwiftUI helps you build great apps on all Apple platforms and is the preferred toolkit for bringing your content into the living room with tvOS 18. Learn how to use SwiftUI to create familiar layouts and controls from TVMLKit, and get tips and best practices.