2

I already reviewed the answer here (ScrollView Doesn't Scroll with Geometry Reader as Child) which is I guess pretty similar but I wasn't able to figure it out.

My Goal is to show the images equally sized (quadratic).

Here's my View:


struct MyGeoView: View {
    
    let icons = ["bed.double.fill","tram.fill","tv.music.note.fill","hare.fill", "person", "clock", "plus", "trash", "home", "arrow", "pencil", "scribble", "folder", "folder.circle", "trash.circle", "paperplane"]
    
    var body: some View {
        
        GeometryReader{ geo in
            ScrollView{
                GeometryReader{ geo in
                    LazyVGrid(columns: Array(repeating: GridItem(.flexible()), count: 3), spacing: 3 ){
                        ForEach(icons, id: \.self){ post in
                            Image(systemName: post)
                                .frame(width: geo.size.width/3, height: geo.size.width/3)
                                .background(Color.pink)
                                .foregroundColor(.white)
                        }
                    }
                }
            }
        }
    }
    
}

Unfortunately it's not scrolling properly (bounces) and always goes back to top. Do you have some ideas?

Thanks!

SwiftUIRookie
  • 591
  • 7
  • 16

1 Answers1

0

You just need only one GeometryReader, external one.

Here is corrected code. Tested with Xcode 12.1 / iOS 14.1

var body: some View {
    GeometryReader{ geo in
        ScrollView{
            LazyVGrid(columns: Array(repeating: GridItem(.flexible()), count: 3), spacing: 3 ){
                ForEach(icons, id: \.self){ post in
                    Image(systemName: post)
                        .frame(width: geo.size.width/3, height: geo.size.width/3)
                        .background(Color.pink)
                        .foregroundColor(.white)
                }
            }
        }
    }
}
Asperi
  • 228,894
  • 20
  • 464
  • 690