我正在尝试使用 ListView 将背景颜色添加到底部安全区域。 我知道如何向列表单元格添加背景颜色,但它不适用于安全区域。有什么办法吗?
注意:我确实尝试了ZStack
和.edgesIgnoringSafeArea
,我需要使用SwiftUI 2.0 List View,不是 LazyVStack
或 SwiftUI 1.0 (iOS 13) ListView
List {
ForEach(0..<100) { index in
Text(String(index))
.listRowBackground(Color.red.edgesIgnoringSafeArea(.all))
}
}
最佳答案
您需要添加:
UITableView.appearance().backgroundColor = .clear
并将List
放入ZStack
中:
@main
struct TestApp: App {
init() {
UITableView.appearance().backgroundColor = .clear
}
var body: some Scene {
WindowGroup {
ZStack {
Color.red.edgesIgnoringSafeArea(.all)
List {
ForEach(0 ..< 100) { index in
Text(String(index))
.listRowBackground(Color.red)
}
}
}
}
}
}
或者,您可以直接设置颜色,而不是使用 ZStack
:
UITableView.appearance().backgroundColor = UIColor(.red)
请注意,这将设置backgroundColor
全局。
关于swift - 如何向底部安全区域的 ListView 添加背景颜色 SwiftUI iOS 14,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64594996/