๐Ÿ‘”ScrollVGridForEach

SwiftUI โŸฉ Layout โŸฉ Grids โŸฉ

๐Ÿ‘” ScrollVGridForEach = ScrollView + LazyVGrid + ForEach

โฌ†๏ธ ้œ€่ฆ๏ผš VGridForEach

// (r) refactor (f) fix
// 2022.02.15 (r) : param `data` -> `indices`
//                : param exchange order `indices` <-> `columns`
//                : param `content` -> `cellViewAtIndex`
//            (f) : @ViewBuilder cellViewAtIndex

import SwiftUI

// ------------------------------
//     ๐Ÿ‘” ScrollVGridForEach
// ------------------------------

/// ๐Ÿ‘” ScrollVGridForEach = ScrollView + LazyVGrid + ForEach
public struct ScrollVGridForEach<Content: View>: View {
    
    // properties
    let indices: Range<Int>
    let columns: [GridItem]
    let alignment  : HorizontalAlignment    // alignment in cell
    let rowSpacing : CGFloat?               // row spacing
    let pinnedViews: PinnedScrollableViews  // ?
    @ViewBuilder let cellViewAtIndex:  (Int) -> Content // โญ ViewBuilder
    
    // view body
    public var body: some View {
        ScrollView(.vertical){              // ScrollView + VGridForEach
            VGridForEach(                   // ๐Ÿ‘” VGridForEach
                indices, 
                columns: columns, 
                alignment: alignment, 
                rowSpacing: rowSpacing, 
                pinnedViews: pinnedViews, 
                cellViewAtIndex: cellViewAtIndex
            ) 
        }
    }
}

// convenience init
extension ScrollVGridForEach {
    public init(
        _ indices: Range<Int>,
        columns  : [GridItem],
        alignment: HorizontalAlignment     = .center,
        rowSpacing : CGFloat?              = nil,
        pinnedViews: PinnedScrollableViews = .init(),
        // โญ ViewBuilder
        @ViewBuilder cellViewAtIndex: @escaping (Int) -> Content
    ) {
        // ForEach
        self.indices = indices
        // LazyVGrid
        self.columns     = columns
        self.alignment   = alignment
        self.rowSpacing  = rowSpacing
        self.pinnedViews = pinnedViews
        self.cellViewAtIndex = cellViewAtIndex
    }
}

History

  1. 2022.02.15

Last updated