首页 > 解决方案 > 列表部分标题的 SwiftUI 2 清除背景

问题描述

SwiftUI 2 破坏了我的应用程序的一部分,该部分依赖于列表部分标题的清晰背景。以前我依靠这一行来使列表部分清晰。有谁知道如何在 SwiftUI 2 中实现这一点?

UITableViewHeaderFooterView.appearance().tintColor = .clear

这是一个适用于 SwiftUI 1 的示例:

struct ContentView: View {

    var body: some View {
        List {
            Section(header:
                Text("List Header")
            ) {
                Text("Hi")
            }
            .listRowBackground(Color.clear)
        }
        .onAppear {
            UITableViewCell.appearance().backgroundColor = UIColor.clear
            UITableViewHeaderFooterView.appearance().tintColor = .clear
        }
    }
}

期望:List Header应该是透明的而不是灰色的。

截屏

标签: iosswiftuiswiftui-listios14

解决方案


以下代码将为您提供一个与系统背景颜色相同的空标题,如果您想向其中添加文本,您可以替换Rectangle甚至Text("Your header")使用StackView

List {
                
                Section(header:
                            Rectangle()
                            .foregroundColor(.clear)
                            .listRowInsets(EdgeInsets())
                            .background(Color(.systemBackground))){
         
                        //content of the list
                        Text("Item x")
                        //...

               }

      }

推荐阅读