0

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)
    }
}

enter image description here

  • 2
    With explicit sizes given like that (`frame(width: 176, height: 110)`), I don't think there's any way you *won't* get overlap on an iPhone screen, which doesn't have enough room for all of the objects. – jnpdx May 05 '21 at 16:09
  • Thank you. :-). – user14119170 May 06 '21 at 12:45

0 Answers0