Created
January 18, 2020 11:13
-
-
Save CodeSlicing/9266dc4cd23d58e81d4cc52999de2def to your computer and use it in GitHub Desktop.
Demo showing a grid view layout utility
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// GridViewDemo.swift | |
// | |
// Permission is hereby granted, free of charge, to any person obtaining a copy | |
// of this software and associated documentation files (the "Software"), to deal | |
// in the Software without restriction, including without limitation the rights | |
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies | |
// of the Software, and to permit persons to whom the Software is furnished to do so, | |
// subject to the following conditions: | |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, | |
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A | |
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | |
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN | |
// AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
// | |
// Created by Adam Fordyce on 18/01/2020. | |
// Copyright © 2020 Adam Fordyce. All rights reserved. | |
// | |
import SwiftUI | |
import PureSwiftUI | |
struct GridViewDemo: View { | |
var body: some View { | |
GridView(3, 6, spacing: 10) { column, row in | |
GridView(3, 3, spacing: 10) { column, row in | |
Text("\(column),\(row)") | |
.frame(30) | |
.clipRoundedRectangleWithStroke(5, .black, lineWidth: 1) | |
} | |
.padding(8) | |
.background(Color.red.opacity(0.3)) | |
.hueRotation(((column + row * 3) * 20).degrees) | |
.cutoutRoundedRectangle(10) | |
} | |
} | |
} | |
struct GridView<Content: View>: View { | |
let columns: Int | |
let rows: Int | |
let spacing: CGFloat | |
let content: (Int, Int) -> Content | |
init(_ columns: Int, _ rows: Int, @ViewBuilder content: @escaping (Int, Int) -> Content) { | |
self.init(columns, rows, spacing: 0, content: content) | |
} | |
init<T: UINumericType>(_ columns: Int, _ rows: Int, spacing: T, @ViewBuilder content: @escaping (Int, Int) -> Content) { | |
self.columns = columns | |
self.rows = rows | |
self.spacing = spacing.asCGFloat | |
self.content = content | |
} | |
var body: some View { | |
VStack(spacing: self.spacing) { | |
ForEach(0..<self.rows) { row in | |
HStack(spacing: self.spacing) { | |
ForEach(0..<self.columns) { column in | |
self.content(column, row) | |
} | |
} | |
} | |
} | |
} | |
} | |
struct GridView_Previews: PreviewProvider { | |
struct GridView_Harness: View { | |
var body: some View { | |
GridViewDemo().previewDevice(.iPhone_11_Pro_Max) | |
} | |
} | |
static var previews: some View { | |
GridView_Harness() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment