Skip to content

Instantly share code, notes, and snippets.

@tera-ny
Last active November 21, 2020 15:04
Show Gist options
  • Save tera-ny/4f503f9f0d84cbcdb38ffb7fd3000207 to your computer and use it in GitHub Desktop.
Save tera-ny/4f503f9f0d84cbcdb38ffb7fd3000207 to your computer and use it in GitHub Desktop.
Propose a product cell layout that supports aspect-fill images with SwiftUI components
struct ProductGridCell: View {
let name: String
let price: Int
let image: UIImage
var body: some View {
VStack(alignment: .leading, spacing: 3) {
GeometryReader(content: { geometry in
Image(uiImage: image)
.resizable()
.scaledToFill()
.frame(width: geometry.size.width, height: geometry.size.height)
})
.aspectRatio(1, contentMode: .fit)
.clipShape(RoundedRectangle(cornerRadius: 4))
Text(name)
.foregroundColor(.primary)
.font(.caption)
Text("¥\(price)")
.font(.caption2)
.foregroundColor(.secondary)
}
}
}
struct ProductGridCell_Previews: PreviewProvider {
static var previews: some View {
ScrollView {
LazyVGrid(columns: [GridItem(), GridItem(), GridItem()], content: {
ForEach(0..<20) { _ in
ProductGridCell(name: "どら焼き", price: 300, image: UIImage(imageLiteralResourceName: "product"))
}
}).padding()
}
}
}
@tera-ny
Copy link
Author

tera-ny commented Nov 21, 2020

Image from Gyazo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment