SwiftUI keeping scroll offset when hiding/showing sidebar and / or resizing window

Viewed 25

I am looking to replicate finder functionality in SwiftUI. Currently I have ScrollView that has content inside it, but when I resize window the ScrollView scrolls and does not keep its initial scroll offset. Is there a way to keep the offset?

Here is a small example of what I have.

    ScrollView([.horizontal, .vertical], showsIndicators: true) {
        VStack {
            HStack {
                ForEach(1...100, id: \.self) { num in
                    Text("num")
                }
            }
            .frame(width: 1500, height: 200, alignment: .topLeading)
            .background(Color.red)
            HStack {
                ForEach(1...100, id: \.self) { num in
                    Text("duo")
                }
            }
            .frame(width: 1500, height: 200, alignment: .topLeading)
            .background(Color.blue)
            HStack {
                ForEach(1...100, id: \.self) { num in
                    Text("tre")
                }
            }
            .frame(width: 1500, height: 200, alignment: .topLeading)
            .background(Color.green)
            HStack {
                ForEach(1...100, id: \.self) { num in
                    Text("quat")
                }
            }
            .frame(width: 1500, height: 200, alignment: .topLeading)
            .background(Color.gray)
        }.frame(width: 1500, height: 1000, alignment: .topLeading)
    }
1 Answers
Related