在watchOS中使用.listRowBackground时如何将cornerRadius添加到SwiftUI列表单元格?

leo*_*lig 7 watchkit swiftui

我试图List通过每个单元格上的图像来获得手表上的正常值listRowBackground

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

我尝试background在单元格View本身中设置修改,但这会导致同样的问题。查看可视化视图调试器,背景图像似乎远远超出了单元格本身。

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)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

图片: 带背景图片:

带背景图像

图片: 无背景图片:

无背景图像

Dim*_*ski 16

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

List {
         Text("Sample cell text")
            .listRowBackground(
               Color(.blue)
               .clipped()
               .cornerRadius(10))
      }
Run Code Online (Sandbox Code Playgroud)


leo*_*lig 0

我想到了!

我将图像包裹在 a 内GeometryReader并将clipped()和添加cornerRadius(10)GeometryReader.

并添加buttonStyle(PlainButtonStyle())NavigationLink.

    private func backgroundImage() -> some View {
        return GeometryReader { g in
            Image(<image>)
                .resizable()
                .blur(radius: 2)
                .scaledToFill()
                .frame(width: g.size.width, height: g.size.height)
        }
        .clipped()
        .cornerRadius(10)
    }
Run Code Online (Sandbox Code Playgroud)

然后添加.listRowBackground(self.backgroundImage())NavigationLink.