Search code examples
xcodeuiscrollviewswiftuiscrollviewxcode11

SwiftUI reverse ScrollView from bottom to top


I want to make a ScrollView that will give me the possibility to scroll from bottom to top. Basically I have a gradient background (bottom - black, top - white). When I set the ScrollView I can scroll down from white to black. I want the app to start at the bottom of ScrollView, to give me the possibility to scroll upwards. (From Black to White)

Thank you!

Current code:

struct ContentView: View {
var body: some View {


    ScrollView(.vertical/*, showsIndicators: false*/){
        VStack(spacing: 0) {
            ForEach ((0..<4).reversed(), id: \.self) {
                Image("background\($0)").resizable()
                    .aspectRatio(contentMode: .fill)
                    .frame(width: UIScreen.main.bounds.width ,height:1000)
            }
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity)
        .background(Color.red)
    }
    .edgesIgnoringSafeArea(.all)

}   

}


Solution

  • I was able to achieve this with a StackView inside a UIScrollView. Put whatever you want in the stackview then in the ViewController.viewDidLoad do

    //flip the scrollview
    scrollview.transform = CGAffineTransform(rotationAngle: Double.pi)
    //flip the stackview
    stackview.transform = CGAffineTransform(rotationAngle: Double.pi)
    

    Now the scrollview will scroll from the bottom up