Skip to content

Instantly share code, notes, and snippets.

@bok-
Last active July 24, 2019 12:12
Show Gist options
  • Save bok-/7112f1a39de7c0a67054ba745d94af92 to your computer and use it in GitHub Desktop.
Save bok-/7112f1a39de7c0a67054ba745d94af92 to your computer and use it in GitHub Desktop.
//
// GridTest.swift
//
import SwiftUI
struct GridTest: View {
var items: [String]
var body: some View {
NavigationView {
List {
ForEach(0..<3) { index in
GeometryReader { geometry in
HStack {
self.items[safe: index * 2]
.map {
Text($0)
.frame(width: geometry.size.width / 2, height: geometry.size.width / 2)
}
self.items[safe: index * 2 + 1]
.map {
Text($0)
.frame(width: geometry.size.width / 2, height: geometry.size.width / 2)
}
}
}
}
}
.listStyle(.grouped)
.navigationBarTitle("Grid Test", displayMode: .inline)
}
}
}
#if DEBUG
struct GridTest_Previews: PreviewProvider {
static var previews: some View {
GridTest(items: [ "First Label", "Second Label", "Third Label", "Fourth Label", "Fifth Label" ])
}
}
#endif
extension Collection {
subscript(safe index: Index) -> Iterator.Element? {
guard index >= startIndex && index < endIndex else { return nil }
return self[index]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment