SwiftUI-Flow icon indicating copy to clipboard operation
SwiftUI-Flow copied to clipboard

VFlow does not work with maxHeight

Open MattNewberry opened this issue 1 year ago • 4 comments

The readme states to set the maxHeight to infinity, however I'm only able to get VFlow to work correctly after specifying an exact height value.

VFlow {
    ForEach(colors, id: \.description) { color in
        RoundedRectangle(cornerRadius: 10)
            .fill(color.gradient)
            .frame(width: 50, height: Double.random(in: 40...60))
    }
}
.frame(maxHeight: 300)

However, this produces: Screenshot 2024-04-23 at 12 58 32 PM

If I change this to a manually specified height, I'm able to correctly see the expected result:

VFlow {
    ForEach(colors, id: \.description) { color in
        RoundedRectangle(cornerRadius: 10)
            .fill(color.gradient)
            .frame(width: 50, height: Double.random(in: 40...60))
    }
}
.frame(height: 300)
Screenshot 2024-04-23 at 12 58 11 PM

The problem here, though, is knowing the height ahead of time to specify.

MattNewberry avatar Apr 23 '24 16:04 MattNewberry

The readme states to set the maxHeight to infinity

Where does README.md state this?

tevelee avatar Apr 23 '24 19:04 tevelee

Just to double check, I created an empty mac app with your example:

import SwiftUI
import Flow

@main
struct ExampleApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

struct ContentView: View {
    var body: some View {
        let colors: [Color] = [
            .blue,
            .orange,
            .green,
            .yellow,
            .brown,
            .mint,
            .indigo,
            .cyan,
            .gray,
            .pink
        ]
        VFlow {
            ForEach(colors, id: \.description) { color in
                RoundedRectangle(cornerRadius: 10)
                    .fill(color.gradient)
                    .frame(width: 50, height: Double.random(in: 40...60))
            }
        }
        .frame(maxHeight: 300)
    }
}

#Preview {
    ContentView()
}

and the result looks like this: image

tevelee avatar Apr 23 '24 19:04 tevelee

My mistake! Let me rephrase - without setting a maxHeight, VFlow does not work correctly. Am I thinking of this incorrectly? I'm picturing this taking up all the height it needs but being constrained by width, instead.

MattNewberry avatar Apr 30 '24 16:04 MattNewberry

Correct. If we do not set a maximum, it indeed takes up all the available space. It's a deliberate design decision. In this regard the builtin HStack/VStack works the same way, unless there's a maximum, it takes up all the available space and centers its content.

tevelee avatar May 02 '24 12:05 tevelee