Switch masonry layouts at runtime using AnyLayout
mainVMasonry and HMasonry both conform to the SwiftUI Layout protocol, you can use AnyLayout to switch between different masonry orientations or other layouts dynamically at runtime.repository·main·Indexed 41 days ago
https://github.com/ciaranrobrien/swiftuimasonryA 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.
VMasonry and HMasonry both conform to the SwiftUI Layout protocol, you can use AnyLayout to switch between different masonry orientations or other layouts dynamically at runtime.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
}Install the library using Swift Package Manager and import the module to begin using the masonry layouts in your SwiftUI views.
import SwiftUIMasonryUse 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
}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
}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
}
}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
}
}