swift - 如何向底部安全区域的 ListView 添加背景颜色 SwiftUI iOS 14

标签 swift swiftui ios14 swiftui-list

我正在尝试使用 ListView 将背景颜色添加到底部安全区域。 我知道如何向列表单元格添加背景颜色,但它不适用于安全区域。有什么办法吗?

注意:我确实尝试了ZStack.edgesIgnoringSafeArea,我需要使用SwiftUI 2.0 List View,不是 LazyVStackSwiftUI 1.0 (iOS 13) ListView

List {
  ForEach(0..<100) { index in
     Text(String(index))
     .listRowBackground(Color.red.edgesIgnoringSafeArea(.all))
  }
}

enter image description here

最佳答案

您需要添加:

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/

相关文章:

ios - ScrollViewDelegate 不调用 scrollViewDidScroll 或 scrollViewWillBeginDragging

swift - UISwitch 未解除分配

swiftui - 使用 NavigationLink 将信息传递到另一个 View

ios - SwiftUI TabBar 省略号不垂直居中

ios - 将 Swift 的 Vision 库识别的文本分配给结构体的实例属性以进行显示时出现问题

SwiftUI 侧边栏不记得状态

ios - 当缩放或移动 iosmap 时,它总是跳回到用户位置 Swift

swift - 将 documentSnapshot 分配给自定义对象?

ios - 尽管 "string"为真,但 UIPasteBoard "hasStrings"属性返回 nil

ios - 如何在 XCUITest 中访问 iOS 14 时间选择器