SwiftUI LazyVGrid例子

FDB43DDE-FF06-4497-8533-0DABAB14DBCF.png

示例代码

import SwiftUI

struct Test: View {
    @State private var targeted = true
    
    var body: some View {
        let fixedColumns = [
            GridItem(.fixed(80)),
            GridItem(.fixed(80)),
            GridItem(.fixed(80)),
            GridItem(.fixed(80))]
        
        let flexibleColumns = [
            GridItem(.flexible(minimum: 60, maximum: 80)),
            GridItem(.flexible(minimum: 60, maximum: 80)),
            GridItem(.flexible(minimum: 60, maximum: 80)),
            GridItem(.flexible(minimum: 60, maximum: 80)),]
        
        let adaColumns = [
            GridItem(.adaptive(minimum: 60, maximum: 80))]
        
        VStack {
            LazyVGrid(columns: fixedColumns) {
                ForEach(0...10, id: \.self) { tag in
                    Text("\(tag)")
                        .font(.subheadline)
                        .padding(.vertical, 8)
                        .frame(maxWidth: .infinity)
                        .background(
                            Color.init(hexString: "#dddddd")
                        )
                        .cornerRadius(4)
                }
            }
            .frame(maxWidth: .infinity)
            
            Divider()
            
            LazyVGrid(columns: flexibleColumns) {
                ForEach(0...10, id: \.self) { tag in
                    Text("\(tag)")
                        .font(.subheadline)
                        .padding(.vertical, 8)
                        .frame(maxWidth: .infinity)
                        .background(
                            Color.init(hexString: "#dddddd")
                        )
                        .cornerRadius(4)
                }
            }
            .frame(maxWidth: .infinity)
            
            Divider()
            
            LazyVGrid(columns: adaColumns) {
                ForEach(0...10, id: \.self) { tag in
                    Text("\(tag)")
                        .font(.subheadline)
                        .padding(.vertical, 8)
                        .frame(maxWidth: .infinity)
                        .background(
                            Color.init(hexString: "#dddddd")
                        )
                        .cornerRadius(4)
                }
            }
            .frame(maxWidth: .infinity)
        }
    }
}

struct Test_Previews: PreviewProvider {
    static var previews: some View {
        Test()
    }
}

你可能感兴趣的:(SwiftUI LazyVGrid例子)