繁体   English   中英

SwiftUI 2 列表部分的清晰背景 header

[英]SwiftUI 2 clear background for list section header

SwiftUI 2 打破了我的应用程序的一部分,该部分依赖于列表部分的清晰背景 header。以前我依靠这一行来使列表部分清晰。 有谁知道如何在 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应该是透明的而不是灰色的。

截屏

下面的代码会给你一个空的 header 与系统相同的背景颜色如果你想向它添加文本你可以用Text("Your header")替换Rectangle或者甚至使用Stack View

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

               }

      }

找到的解决方案:诀窍是使用带有固定部分的 LazyVStack:

struct ContentView: View {
    var body: some View {
        ScrollView {
            LazyVStack(pinnedViews:[.sectionHeaders]) {
                Section(header: Text("List Header")) {
                    Text("Hi")
                    Text("Hi")
                    Text("Hi")
                }
            }
        }
    }
}

这是一个可能的解决方法(虽然不适用于清晰的颜色):

struct ContentView: View {
    var body: some View {
        List {
            Section(header: header) {
                Text("Item 1")
                Text("Item 2")
                Text("Item 3")
            }
            Section(header: header) {
                Text("Item 1")
                Text("Item 2")
                Text("Item 3")
            }
        }
    }

    var header: some View {
        HStack {
            Text("Header")
            Spacer()
        }
        .padding(5)
        .background(Color.blue)
        .listRowInsets(EdgeInsets())
    }
}

SwiftUI 2破坏了我的应用程序的一部分,该部分依赖于清晰的背景来显示List部分标题。 以前,我依靠这一行来使列表部分清楚。 有谁知道如何在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应该透明而不是灰色。

截屏

暂无
暂无

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

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