美文网首页
SwiftUI LazyVGrid例子

SwiftUI LazyVGrid例子

作者: xo1988 | 来源:发表于2022-08-22 14:52 被阅读0次
    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例子

          本文链接:https://www.haomeiwen.com/subject/beyigrtx.html