Last active
October 20, 2021 20:57
-
-
Save edudnyk/08d82f3579bb04b46e05d52170828e5e to your computer and use it in GitHub Desktop.
An example of usage of the SolidScrollView in SwiftUI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct ContentView: View { | |
var config = ScrollViewConfig() | |
@State var scrollViewProxy: SolidScrollViewProxy? | |
var body: some View { | |
VStack(spacing: 0) { | |
SolidScrollView(config) { | |
VStack(spacing: 0) { | |
Color.red.frame(height: 200) | |
Color.green.frame(height: 200) | |
Color.blue.frame(height: 200) | |
Color.black.frame(height: 200) | |
} | |
} | |
Button("Scroll to 3/4 of the content height via proxy") { | |
guard let scrollViewProxy = scrollViewProxy else { return } | |
let contentOffset = CGPoint(x: 0, y: min(scrollViewProxy.contentSize.height * 3.0 / 4, scrollViewProxy.maxContentOffset.y)) | |
scrollViewProxy.setContentOffset(contentOffset, animated: true, completion: { completed in | |
print("Scrolled via proxy to \(contentOffset)! Completed: \(completed)") | |
}) | |
} | |
} | |
.onPreferenceChange(ContainedScrollViewKey.self) { | |
scrollViewProxy = $0 | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment