【问题标题】:How to add background color to the list view of bottom safe area SwiftUI iOS 14如何为底部安全区域的列表视图添加背景颜色SwiftUI iOS 14
【发布时间】:2021-02-12 03:01:27
【问题描述】:
我正在尝试使用列表视图将背景颜色添加到底部安全区域。
我知道如何为列表单元格添加背景颜色,但它不适用于安全区域。有什么办法吗?
注意:我确实尝试过ZStack 和.edgesIgnoringSafeArea,我需要使用SwiftUI 2.0 列表视图,而不是LazyVStack 或SwiftUI 1.0 (iOS 13) List View
List {
ForEach(0..<100) { index in
Text(String(index))
.listRowBackground(Color.red.edgesIgnoringSafeArea(.all))
}
}
【问题讨论】:
标签:
swift
swiftui
ios14
swiftui-list
【解决方案1】:
你需要添加:
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 全局。