0

I'm working with a vertical ScrollView in SwiftUI, and I want to have dynamic resizing for two components inside it. Specifically, I want one component to have a constrained maxWidth and maxHeight (up to a certain limit), while the other component should have a flexible width and height that takes up the remaining space.

However, due to the nature of ScrollView—where its maxHeight is effectively .infinity—it seems challenging to constrain one component dynamically.

Without the ScrollView, resizing works as expected, but with ScrollView, I have to set fixed width and height, which removes the dynamic behavior.

Without ScrollView, letting SwiftUI resize:

VStack(spacing: .zero) {
    ViewWithMaxSize()
        .frame(maxWidth: UIScreen.main.bounds.width * 0.5, maxHeight: UIScreen.main.bounds.height * 0.3)

    PriorityView()
        .frame(maxWidth: .infinity, maxHeight: .infinity)

}

With ScrollView, forced to set a fixed width / height, no longer dynamic:

VStack(spacing: .zero) {
    ScrollView {
        ViewWithMaxSize()
            .frame(width: UIScreen.main.bounds.width * 0.5, height: UIScreen.main.bounds.height * 0.3)

        PriorityView()
            .frame(maxWidth: .infinity, maxHeight: .infinity)
    }
} 

Is there a way to maintain dynamic resizing behavior for both components inside a ScrollView, where one has limited size and the other expands?

I've tried wrapping the ScrollView in a GeometryReader to constrain the frame of the ScrollView to the space available on the screen. ViewWithMaxSize becomes too small (almost no height).

2
  • What do you mean by "the other component should have a flexible width and height that takes up the remaining space"? "Remaining space" is not well-defined in a scroll view, since the scroll content can be arbitrarily large. Do you mean the view should expand to the minimum size such that the scroll view's own bounds is completely covered? In that case the scroll content will always be the same size as the scroll view itself, so why do you want to use a ScrollView in the first place? Do you just want the scroll bounce behaviour? Commented Sep 15, 2024 at 12:45
  • Yes that’s correct Sweeper. If the content on the screen doesn’t overflow, I’m using the bounce so that the ScrollView isn’t “scrollable”. The view looks great without a ScrollView but when trying to adopt Dynamic Type it inevitably overflows. I would prefer the ViewWithMaxSize to shrink based on the size of the PriorityView but I guess this is the tradeoff that we have to live with then? Commented Sep 15, 2024 at 12:55

0

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.