Skip to content

Instantly share code, notes, and snippets.

@NeilsUltimateLab
Last active February 17, 2022 13:58
Show Gist options
  • Save NeilsUltimateLab/339cd0b4c514f07bcb08db9469be1e27 to your computer and use it in GitHub Desktop.
Save NeilsUltimateLab/339cd0b4c514f07bcb08db9469be1e27 to your computer and use it in GitHub Desktop.
A SwiftUI Scrollview container starts from right (horizontally) or bottom (vertically).
import SwiftUI
struct BackwardScrollView<V: View>: View {
var axis: Axis.Set = .horizontal
var content: ()->V
var leadingInset: CGFloat
init(_ axis: Axis.Set = .horizontal, leadingInset: CGFloat = 10, content: @escaping ()->V) {
self.axis = axis
self.content = content
self.leadingInset = leadingInset
}
var body: some View {
GeometryReader { proxy in
ScrollView(axis) {
Stack(axis) {
Spacer(minLength: leadingInset)
content()
}
.padding()
.frame(minWidth: proxy.size.width)
}
}
}
}
struct Stack<V: View>: View {
var axis: Axis.Set = .horizontal
var content: V
init(_ axis: Axis.Set = .horizontal, @ViewBuilder buider: ()->V) {
self.axis = axis
self.content = buider()
}
var body: some View {
switch axis {
case .horizontal:
HStack {
content
}
case .vertical:
VStack {
content
}
default:
VStack {
content
}
}
}
}
struct ContentView: View {
var body: some View {
BackwardScrollView(.horizontal, leadingInset: 50) {
ForEach((1...50).reversed(), id: \.self) { item in
Text("Hello \(item)")
.frame(maxHeight: .infinity)
Divider()
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment