SwiftUI Masonry

repository·main·Indexed 41 days ago

https://github.com/ciaranrobrien/swiftuimasonry

A library providing VMasonry and HMasonry layout containers for Pinterest-style grids in SwiftUI. Supports vertical and horizontal orientations, customizable spacing, and placement modes (.fill and .order). Includes .masonryColumnSpan() and .masonryRowSpan() modifiers for spanning multiple columns or rows, and conforms to the SwiftUI Layout protocol for use with AnyLayout.

Tokens
812
Snippets
6
Records
7
Agent score
3%

What's inside SwiftUI Masonry

  1. Configure Masonry placement modes

    main

    Control how subviews are placed within the masonry grid using the placementMode parameter with a MasonryPlacementMode value:

    • .fill: (Default) Places each subview in the column or row with the most available space.
    • .order: Places each subview according to its order in the view tree.
    VMasonry(columns: 2, spacing: 12, placementMode: .fill) {
        // Masonry content
    }
  2. Span columns or rows with masonry modifiers

    main

    Use the .masonryColumnSpan() or .masonryRowSpan() view modifiers to allow a subview to occupy more than one column or row. By default, subviews span 1 column/row.

    These modifiers accept either an Int or a MasonryColumnCount/MasonryRowCount enum with two cases:

    • .fixed(Int): A constant number of columns/rows.
    • .adaptive(minWidth: CGFloat) / .adaptive(minHeight: CGFloat): A variable number based on constraints.
    VMasonry(columns: .adaptive(minWidth: 128)) {
        // Other masonry content
    
        MyCellView()
            .masonryColumnSpan(.fixed(2))
        
        // Other masonry content
    }
  3. Configure spacing in masonry layouts

    main

    You can control the distance between adjacent subviews using several parameters:

    • spacing: A single value for uniform spacing.
    • horizontalSpacing: Specific spacing for horizontal gaps.
    • verticalSpacing: Specific spacing for vertical gaps.
    VMasonry(columns: 2, horizontalSpacing: 8, verticalSpacing: 12, placementMode: .order) {
        // Masonry content
    }
  4. Use HMasonry for horizontal masonry layouts

    main

    Use HMasonry to arrange subviews in a horizontal Pinterest-like grid. It is typically wrapped in a horizontal ScrollView.

    Parameters:

    • rows: The number of rows in the masonry.
    • spacing: The distance between adjacent subviews (use nil for default distance).
    • placementMode: The placement mode for subviews in this masonry.
    ScrollView(.horizontal) {
        HMasonry(rows: 2) {
            // Masonry content
        }
    }
  5. Use VMasonry for vertical masonry layouts

    main

    Use VMasonry to arrange subviews in a vertical Pinterest-like grid. It is typically wrapped in a vertical ScrollView.

    Parameters:

    • columns: The number of columns in the masonry.
    • spacing: The distance between adjacent subviews (use nil for default distance).
    • placementMode: The placement mode for subviews.
    ScrollView(.vertical) {
        VMasonry(columns: 2) {
            // Masonry content
        }
    }