首页 > 解决方案 > 在watchOS中使用.listRowBackground时如何将cornerRadius添加到SwiftUI List Cell?

问题描述

我正在尝试在每个单元格上使用图像来获得List手表上的正常值。listRowBackground

但是当我将图像设置为listRowBackground标准的角半径List消失时(见下文)。

我试图background在 Cell-View本身中设置修改,但这会导致同样的问题。查看 Visual View Debugger,背景图像似乎远远超出了单元格本身。

struct ListView: View {
    @ObservedObject var model: ListModel

    var body: some View {
        List {
            ForEach(self.model.items) { item in
                NavigationLink(destination: PlayerView(item: item)) {
                    ListCell(item: item).frame(height: 100)
                }
                .listRowBackground(Image(uiImage: item.image)
                .resizable()
                .scaledToFill()
                .opacity(0.7)
                )
            }
        }
        .listStyle(CarouselListStyle())
        .navigationBarTitle(Text("Today"))

    }
}

@available(watchOSApplicationExtension 6.0, *)
struct ListCell: View {
    var item: ListItem

    var body: some View {
        VStack(alignment: .leading) {
            Text("\(self.item.length) MIN . \(self.item.category)")
                .font(.system(.caption, design: .default))
                .foregroundColor(.green)
                .padding(.horizontal)
                .padding(.top, 2)
            Text(self.item.title)
                .font(.headline)
                .lineLimit(2)
                .padding(.horizontal)
        }
    }
}

图片:带背景图片:

带背景图片

图片:无背景图片:

无背景图片

标签: watchkitswiftui

解决方案


这适用于我Xcode 11.5,只需添加.clipped()and .cornerRadius(10)修饰符。

List {
         Text("Sample cell text")
            .listRowBackground(
               Color(.blue)
               .clipped()
               .cornerRadius(10))
      }

推荐阅读