Last active
February 17, 2022 13:58
-
-
Save NeilsUltimateLab/339cd0b4c514f07bcb08db9469be1e27 to your computer and use it in GitHub Desktop.
A SwiftUI Scrollview container starts from right (horizontally) or bottom (vertically).
This file contains hidden or 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
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 | |
} | |
} | |
} | |
} |
This file contains hidden or 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 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