I am following Apple's WWDC2020 SwiftUI video "What's new in SwiftUI"
This one: https://developer.apple.com/videos/play/wwdc2020/10041/
I have opened this video in Apple's "Apple Developer" App, which provides the demo code. I just copied this original code into Xcode and it's not working properly (screenshot below), why?
Original Code:
struct ContentView: View {
var items: [Item]
var body: some View {
ScrollView {
LazyVGrid(columns: Array(repeating: GridItem(), count: 4)]) {
ForEach(items) { item in
ItemView(item: item)
}
}
.padding()
}
}
}
struct Item: Identifiable {
var name: String
var id = UUID()
var icon: Image {
Image(systemName: name)
}
var color: Color {
colors[colorIndex % (colors.count - 1)]
}
private static var nextColorIndex: Int = 0
private var colorIndex: Int
init(name: String) {
self.name = name
colorIndex = Self.nextColorIndex
Self.nextColorIndex += 1
}
}
struct ItemView: View {
var item: Item
var body: some View {
ZStack {
RoundedRectangle(cornerRadius: 8, style: .continuous)
.fill()
.layoutPriority(1)
.foregroundColor(item.color)
item.icon
.resizable()
.aspectRatio(contentMode: .fit)
.padding(.all, 16)
.foregroundColor(.white)
}
.frame(width: 176, height: 110)
}
}