0

I have this SwiftUI code that builds a large UI view that's too big to fit on screen, so I've put it in a 2D scrollview. While this example is a grid, let's assume the content fills just as much but the items are more randomly placed. When the view appears, I want it to start with having one of these items centered, in the case below the item with ID "(25,25)". This compiles fine, and looks fine by the docs, and runs, but does not scroll to the ZStack with ID "(25,25)". Why does it not scroll to this ZStack? The code was tested with both iOS 14 and iOS 15b1.

var body: some View {
    ScrollViewReader { scrollView in
        ScrollView([.horizontal, .vertical]) {
            VStack(alignment: .leading, spacing: 0) {
                ForEach(0..<50) { y in
                    HStack(alignment: .center, spacing: 0) {
                        ForEach(0..<50) { x in
                            ZStack {
                                Circle()
                                    .frame(width: 35, height: 35)
                                    .foregroundColor(.blue.opacity(0.5))
                                Text("(\(x),\(y)")
                                    .font(.system(size: 8))
                            }
                            .offset(x: CGFloat(x * 25), y: CGFloat(y * 25))
                            .id("(\(x),\(y))")
                        }
                    }
                }
            }
            
        }
        .onAppear {
            DispatchQueue.main.asyncAfter(deadline: .now() + 3.0) {
                scrollView.scrollTo("(25,25)")
            }
        }
    }
}
niklassaers
  • 8,480
  • 20
  • 99
  • 146
  • I think it is scrolling, but the `offset` value is making it hard to see, if u remove the `offset(x: y:)`, you can see that the scroll is working – cedricbahirwe Jun 14 '21 at 11:19
  • I use the offset to place the shapes in the view, so I need it to scroll to where it's offset to. Or have an alternative to offset to place it – niklassaers Jun 14 '21 at 17:32

1 Answers1

0

I think you're mistaking offset for padding, with just padding it works.


It does look like the not scrolling to a view with offset is a bug though.

Theoretically your code has a mistake: You add an ID to the view before the offset, so when you scroll to that ID it scrolls to where the view was before the offset.

Switch offset and id around and it should work (it doesn't though which makes me think there is a SwiftUI bug somewhere here).

Casper Zandbergen
  • 3,419
  • 2
  • 25
  • 49
  • Thanks for pointing out the offset before id, I'll update the code above. However, even with that fix, it still doesn't scroll. I replaced offset with padding, but it still didn't scroll. If it does for you, could you show me how? I'm happy to hear you think it should work to, I'll file a radar – niklassaers Jun 18 '21 at 06:01
  • 1
    @niklassaers I didn't change any of your code other than the order of id & offset and then replaced offset with padding. That made it work for me. If you cannot reproduce that then I'll check again when I have access to my mac on monday. – Casper Zandbergen Jun 19 '21 at 08:11