Saturday, November 21, 2026
HomeiOS Developmentswift - Utilizing SwiftUI's new Grid API in iOS 16 dynamically to...

swift – Utilizing SwiftUI’s new Grid API in iOS 16 dynamically to format an unknown # of views of unequal sizes

[ad_1]

This submit goes into nice element of SwiftUI’s new Grid view in iOS 16, nonetheless it makes use of static examples. I’ve created a demo undertaking beneath to indicate an instance, in my use case I have no idea the precise quantity or size of the strings in knowledge. Is it attainable to make use of this new Grid API to format these views in a manner that can evenly distribute them, i.e. assemble my code in order that, e.g. if 3 view’s don’t match, it can solely make 2 columns and push the third view to the following row in order that they will not overlap?

import SwiftUI

struct ContentView: View {
    var knowledge = [
    "Beatles",
    "Pearl Jam",
    "REM",
    "Guns n Roses",
    "Red Hot Chili Peppers",
    "No Doubt",
    "Nirvana",
    "Tom Petty and the Heart Breakers",
    "The Eagles"
   
    ]
    

    var physique: some View {
        
        Grid() {
            GridRow {
                ForEach(knowledge, id: .self) { bandName in
                    Textual content(bandName)
                        .fixedSize(horizontal: true, vertical: false)
                }
            }
            
        
        }
        
        .padding()
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

enter image description here

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments