I was referred to (https://medium.com/@wendyabrantes/swift-ui-square-image-modifier-3a4370ca561f). But this code only shows the left side, so I added a sentence to the author's code.
.frame(width: geometry.size.width, height: geometry.size.height)
I was referred to (https://medium.com/@wendyabrantes/swift-ui-square-image-modifier-3a4370ca561f). But this code only shows the left side, so I added a sentence to the author's code.
.frame(width: geometry.size.width, height: geometry.size.height)
| 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() | |
| } | |
| } | |
| } |