簡體   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