繁体   English   中英

如何在 SwiftUI 的 InsetGroupedListStyle 列表中为单元组添加边框?

[英]How do I add a border to a cell group in an InsetGroupedListStyle List in SwiftUI?

鉴于以下观点

import SwiftUI

struct ContentView: View {
  
  init() {
    UITableViewCell.appearance().backgroundColor = .black
    UITableView.appearance().backgroundColor = .black
  }
  
  var body: some View {
    List {
      Text("foo").listRowBackground(Color.gray)
      Text("bar").listRowBackground(Color.gray)
      Text("foo").listRowBackground(Color.gray)
      Text("bar").listRowBackground(Color.gray)
    }
    .listStyle(.insetGrouped)
  }
}

呈现如下

在此处输入图像描述

如何在 InsetGroupedListStyle 的圆角之后的列表元素周围添加一个边框,就像这个红色边框一样

在此处输入图像描述

我尝试向列表或单元格添加边框和阴影,但它们与 insetGrouped 单元格“块”不匹配。

以这种方式添加边框将不起作用,因为 Apple 有列表布局标准,我们无法修改这些标准。 为了满足您的要求,我们必须通过将它们分成几个部分并在每个部分中保留 VStack 来进行自己的定制。

在此处输入图像描述 请检查我下面的代码,它为每个section提供了边框。

var body: some View {
      List {
          Section {
              VStack {
                  ForEach(items, id: \.self) { item in
                      HStack {
                          Text(item)
                          Spacer()
                      }
                      .padding(8)
                      
                      if items.last != item {
                          Divider()
                      }
                  }
              }
              .overlay(RoundedRectangle(cornerRadius: 10, style: .circular).stroke(Color(uiColor: .tertiaryLabel), lineWidth: 1)
                              )
          }
      }
      .listStyle(InsetListStyle())
      
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM