微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

SwiftUI:LazyVGrid 间距不正确

如何解决SwiftUI:LazyVGrid 间距不正确

我正在关注 Apple 的 WWDC2020 SwiftUI 视频“SwiftUI 的新功能

这个:https://developer.apple.com/videos/play/wwdc2020/10041/

我已在 Apple 的“Apple Developer”应用程序中打开了此视频,该应用程序提供了演示代码。 我刚刚将这段原始代码复制到 Xcode 中,但它无法正常工作(如下截图),为什么?

原始代码

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。