4
ScrollView {
    VStack() {
        ForEach(0 ..< 5) { item in
            Text("Hello There")
                .font(.largeTitle)
         }
    }
}

The above code results in this:

enter image description here

When we add a GeometryReader, the views lose their sizing and spacing from one another (the .position modifier is just to center the text views within the GeometryReader):

ScrollView {
    VStack() {
        ForEach(0 ..< 5) { item in
            GeometryReader { geometry in
            Text("Hello There")
                .font(.largeTitle)
                .position(x: UIScreen.main.bounds.size.width/2)
            }
        }
    }
}

enter image description here

Can anybody share some help/advice around why this is the behavior and what I may want to look into in order to use a GeometryReader on center aligned views within a ScrollView as I'm trying to do here?

Adding a .frame(minHeight: 40) modifier to the ForEach is one way to force the views to take the space that the Text needs, but that is not a very nice or scalable solution.

Thank you!

4
  • VStack itself centres your Text views, as is visible on your first screenshots, so could you be more specific about what are you trying to do really? Commented Dec 2, 2020 at 15:48
  • @ RanLearns: which dimensions you are trying to read in your project? As I could understand you are trying to read Text size, or you want to read the screen dimensions? Commented Dec 2, 2020 at 18:29
  • @Omid Thanks for commenting. I am trying to read the Text's bounds rectangle (i.e. its minY position) within the ScrollView (0 at the top of the screen, negative if scrolled off the top). I am able to read this as needed using geometry.frame(in: .global).minY using the code posted in my question, but as you see the layout is badly affected. Commented Dec 2, 2020 at 20:28
  • If you just need to read the size, there is other ways as well, unless you want change the view with that size. why you need to read text size? Commented Dec 2, 2020 at 20:39

1 Answer 1

2

If you need just width of screen then place GeometryReader outside of scrollview:

GeometryReader { geometry in     // << here !!
  ScrollView {
    VStack() {
        ForEach(0 ..< 5) { item in
            Text("Hello There")
                .font(.largeTitle)
                .position(x: UIScreen.main.bounds.size.width/2)
            }
        }
    }
}

Can anybody share some help/advice around why this is the behavior

ScrollView does not have own size so GeometryReader cannot read anything from it and provides for own children some minimal dimension.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for responding! In terms of the GeometryReader - when I move it outside of the ScrollView then geometry.frame(in: .local).minY is always 0 and geometry.frame(in: .global).minY is always 48 with safe area (or 0 with ignored SafeArea). I was using the GeometryReader as the container view of the Text because it was allowing me to keep track of the position of Text within the ScrollView. Your answer fixes the layout issue, but does not provide the information needed on the Text view's bounds rectangle
This sounds as you need something like stackoverflow.com/a/62588295/12299030.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.