UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

How do I get the content height of a ScrollView?

Forums > SwiftUI

I recently started using SwiftUI, and one of the things that has caused me many headaches is the ScrollView. The screen I'm currently working basically involves a questionnaire type view with follow up questions appearing when the previous one has been answered. For this purpose, I want the scroll view to scroll down each time a component is added. After trying many different solutions, the one that seemed to work best is flipping the scroll view, and then flipping the contents again, resulting in a base like this:

ScrollView {
  VStack(spacing: 0) {
    // components
  }
  .rotationEffect(.radians(.pi))
  .scaleEffect(x: -1, y: 1, anchor: .center)
}
.rotationEffect(.radians(.pi))
.scaleEffect(x: -1, y: 1, anchor: .center)
.animation(.easeInOut)

So far, so good, however, the flipping of the scrollview produces at lot of UI glitches when the content fits on the screen without the flip. What I'm trying to do, is get the content height of the scroll view, and only execute the flip if the content height > screen height. I'm pretty lost here as to how to properly do this. GeometryReaders inside a ScrollView seem to be very sketchy and not reliable. I tried using preferences to write out the content height to a property, but that doesn't really seem to be called in this use case.

Any help would be much appreciated!

3      

Just wrap your ScrollView in a GeomtryReader as follows -

 GeometryReader { geo in
            ScrollView {
              VStack(spacing: 0) {
                Text("Hello")
                Button("Press") {
                    print(geo.size.height)
                }
              }
              .rotationEffect(.radians(.pi))
              .scaleEffect(x: -1, y: 1, anchor: .center)
            }
            .rotationEffect(.radians(.pi))
            .scaleEffect(x: -1, y: 1, anchor: .center)
            .animation(.easeInOut)
     }

I added the button and the text so you can test it out. Press the button and in the debug preview you will see the height of the ScrollView. Similary you can get its width by using geo.size.width. Hope this helps

3      

Thanks! This does get the height of the scroll view, but what I'm trying to do is to determine whether the content height of the scroll view is bigger than frame. I tried doing so by having a second geometry reader inside the scroll view, but that results in unstable behaviour (various UI glitches, and components inside the scroll view suddenly lose interaction)

3      

Can you post the code for when you tried adding another Geometry Reader?

3      

I replicated it using your sample code, but did the same in the actual app:

GeometryReader { geo in
            ScrollView {
                GeometryReader { inside in
                VStack(spacing: 0) {
                    Text("Hello")
                    Button("Press") {
                        print(inside.size.height)
                    }
                    // Couple more buttons
                }
                .rotationEffect(.radians(.pi))
                .scaleEffect(x: -1, y: 1, anchor: .center)
                }
            }
            .rotationEffect(.radians(.pi))
            .scaleEffect(x: -1, y: 1, anchor: .center)
            .animation(.easeInOut)
        }

It looks like the inner geometry doesn't correctly size to the content, as the inside.size.height always returns 10px, making anything out of that 10px high frame not touchable

3      

Bummer. I would of done the same thing as you. Thats my knowledge exhausted lol. I tried it out like you said and i see what you mean. I have done bit of hunting and Paul has written an article on Geometry Reader that may solve what your trying to do. Give that a read and see how you go - https://www.hackingwithswift.com/books/ios-swiftui/understanding-frames-and-coordinates-inside-geometryreader

3      

Here is a way to actually get the height of the ScrollView content.

struct ContentView: View {

    var body: some View {
        ScrollView {
            ForEach(0..<1000) { i in
                Text("\(i)")
            }
            .frame(maxWidth: .infinity)
            .overlay(
                GeometryReader { proxy in
                    Color.clear.onAppear { print(proxy.size.height) }
                }
            )
        }
    }
}

We get an output of 20500.0 which is correct.

5      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.