Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
482 views
in Technique[技术] by (71.8m points)

ios - Order LazyVGrid Elements vertically rather than horizontally - SwiftUI

I have a LazyVStack which will grow vertically depending on how many elements are in it. However when it is generated, the elements go left to right, then down to the next row etc. I want it to order the elements vertically and then when it has reached the bottom of the column, move to the next and start doing the same again. Is this possible?

Here is the code I have so far

struct Player: Identifiable{
    var id = UUID()
    
    var name: String
}

var players = [Player(name: "Unknown"),
               Player(name: "Player 1"),
               Player(name: "Player 2"),
               Player(name: "Player 3")]... this carries on for 23 players

let columns = [
    GridItem(.flexible(), spacing: 0),
    GridItem(.flexible(), spacing: 0)
]

VStack(spacing: 10){
    LazyVGrid(columns: columns, spacing: 10){
        ForEach(players){player in
            Text(player.name)
                .padding(.trailing)
        }
    }
}
.padding(.leading)

I'm thinking I may need a LazyHGrid instead, but the number of players passed in could change so I want it to be responsive. Any ideas?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...