GeometryReader in SwiftUI ScrollView causes weird behaviour and random offset

Just try to put ScrollView inside GeometryReader and not vice versa:

// ...
    var body: some View {

        GeometryReader { geometry in

            ScrollView {
                Image("hp")
                    .resizable()
                    .aspectRatio(contentMode: .fill)
                    .frame(width: geometry.size.width, height: 400)
                    .clipped()
            }
        }

    }
// ...

and the result should be:

enter image description here


GeometryReader, if placed as you did, provides the size not of ScrollView itself, but of "Content View" in which image is placed. "Content View" of ScrollView is bigger than screen size, to allow spring effect at the edges, when one scrolls more than a scrolling document size.

The correct usage is in @Александр_Грабовский answer.

Tags:

Ios

Swift

Swiftui