How to create grid in SwiftUI

后端 未结 4 1766
孤街浪徒
孤街浪徒 2020-12-21 09:35

I know that we can create a List in vertical SwiftUI like this,

struct ContentView : View {
    var body: some View {
        NavigationView {
            Li         


        
相关标签:
4条回答
  • 2020-12-21 09:45

    Checkout ZStack based example here

    Grid(0...100) { _ in
        Rectangle()
            .foregroundColor(.blue)
    }
    

    0 讨论(0)
  • 2020-12-21 09:48

    You can create your customView like this to achieve UICollectionView behavior:-

    struct ContentView : View {
        var body: some View {
            VStack(alignment: .leading, spacing: 10) {
                ScrollView(showsHorizontalIndicator: true) {
                    HStack {
                        ForEach(0...10) {_ in
                            GridView()
                        }
                    }
                }
                List {
                    ForEach(0...5) {_ in
                        ListView()
                    }
                }
                Spacer()
            }
        }
    }
    
    struct ListView : View {
        var body: some View {
            Text(/*@START_MENU_TOKEN@*/"Hello World!"/*@END_MENU_TOKEN@*/)
            .color(.red)
        }
    }
    
    struct GridView : View {
        var body: some View {
            VStack(alignment: .leading, spacing: 10) {
                Image("marker")
                    .renderingMode(.original)
                    .cornerRadius(5)
                    .frame(height: 200)
                    .border(Color.red)
                Text("test")
            }
        }
    }
    

    0 讨论(0)
  • 2020-12-21 09:58

    Available for iOS/iPadOS 14 on Xcode 12. You can use LazyVGrid to load just what the user see into screen and not the whole list, List is lazy by default.

    import SwiftUI
    
    //MARK: - Adaptive
    struct ContentView: View {
        
        var body: some View {
            ScrollView {
                LazyVGrid(columns: [GridItem(.adaptive(minimum:100))]) {
                    ForEach(yourObjects) { object in
                        YourObjectView(item: object)
                    }
                }
            }
        }
    }
    
    //MARK: - Custom Columns
    struct ContentView: View {
            
        var body: some View {
             ScrollView {   
                 LazyVGrid(columns: Array(repeating: GridItem(), count: 4)) {
                     ForEach(yourObjects) { object in
                         YourObjectView(item: object)
                     }
                 }
             }
        }
    }
    

    Don't forget replace the info objects with your info and YourObjectView with your customView.

    0 讨论(0)
  • 2020-12-21 10:00

    iOS 14

    There is 2 new native Views that you can use:

    1. LazyHGrid

    2. LazyVGrid

    With code or directly from the library:

    The library contains a fully working sample code that you can test yourself.

    0 讨论(0)
提交回复
热议问题