SwiftUI 从下到上反转ScrollView

Van*_*fen 8 xcode scrollview uiscrollview swiftui xcode11

我想制作一个 ScrollView,它可以让我从下到上滚动。基本上我有一个渐变背景(底部 - 黑色,顶部 - 白色)。当我设置 ScrollView 时,我可以从白色向下滚动到黑色。我希望应用程序从 ScrollView 的底部启动,以便我可以向上滚动。(从黑到白)

谢谢你!

当前代码:

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)

}   
Run Code Online (Sandbox Code Playgroud)

}

jon*_*cer 15

我希望滚动视图从下到上而不是从上到下显示。这有我的答案:https ://www.thirdrocktechkno.com/blog/implementing-reversed-scrolling-behaviour-in-swiftui/

TLDR:.rotationEffect(Angle(degrees: 180)).scaleEffect(x: -1.0, y: 1.0, anchor: .center)在滚动视图上以及滚动视图中的项目上

我的完整代码:


ScrollView(.vertical, showsIndicators: true, content: {
                LazyVStack(alignment: .center, spacing: nil, pinnedViews: [], content: {
                    ForEach(1...5, id: \.self) { count in
                        ScrollView(.horizontal, showsIndicators: false, content: {
                            LazyHStack(alignment: .center, spacing: nil, pinnedViews: [], content: {
                                ForEach(1...10, id: \.self) { count in
                                    Text("Placeholder \(count)").rotationEffect(Angle(degrees: 180)).scaleEffect(x: -1.0, y: 1.0, anchor: .center)
                                }
                            })
                        })
                    }
                })
            }).rotationEffect(Angle(degrees: 180)).scaleEffect(x: -1.0, y: 1.0, anchor: .center)

Run Code Online (Sandbox Code Playgroud)


小智 2

我可以通过 UIScrollView 中的 StackView 来实现这一点。将您想要的任何内容放入 stackview 然后放入 ViewController.viewDidLoad 中

//flip the scrollview
scrollview.transform = CGAffineTransform(rotationAngle: Double.pi)
//flip the stackview
stackview.transform = CGAffineTransform(rotationAngle: Double.pi)
Run Code Online (Sandbox Code Playgroud)

现在滚动视图将从下向上滚动