SwiftUI-Flow
SwiftUI-Flow copied to clipboard
VFlow does not work with maxHeight
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:
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)
The problem here, though, is knowing the height ahead of time to specify.
The readme states to set the maxHeight to infinity
Where does README.md state this?
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:
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.
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.