|
Hey everybody, In the new version of SwiftUI we're able to scroll to a position in the ScrollView by using a ScrollViewReader and it's value property. However, I want to be able to have a button outside of the ScrollViewReader that when tapped can scroll the ScrollView to the correct position. I've been trying to find a solution and can't come up with one. Does anybody know how to make this happen? Thanks in advance for the help. |
|
Hey Justin, The trick here is extracting the storage of the target outside of the ScrollView. The following approach is easiest within a single View struct, but it can be extended to work across different View structs by using Bindings. Start off by defining a store property on your struct that holds an optional reference of the target identifier. I'll use
Then set up the skeleton UI. I'll have my main scrollview take up most of the space, but then I'll have a horizontally scrolling row of buttons along the bottom of the screen that scroll to different children of the ScrollView.
Last but not least we need to make use of the property we defined earlier, as well as SwiftUI's new
Hope that helps! --Jakub |
|
|
|
@jakcharvat This was incredibly helpful. Thank you for posting this detailed solution. |
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.