【问题标题】:How do I change the color around the list area?如何更改列表区域周围的颜色?
【发布时间】:2021-09-03 14:19:51
【问题描述】:

在 SwiftUI 中,我将如何更改列表周围的背景颜色?我尝试过使用各种ZStack.listRowBackground(Color.clear),但没有运气。这是一些示例代码。

struct SwiftUIView: View {

    var body: some View {
        ZStack(alignment: .top) {
            Color(.blue)
            VStack {
                List(0 ..< 5) { item in
                    HStack {
                        Image(systemName: "clock")
                        Text("Placeholder")
                    }
                }
            }
        }
    }
}

【问题讨论】:

  • 这是否回答了您的问题stackoverflow.com/a/65093021/12299030
  • 好吧,也许我遗漏了一些东西,但listRowBackground 只是更改了列表背景颜色,而不是列表周围的区域。

标签: swiftui swiftui-list


【解决方案1】:

有两种方法可以做到这一点:

  1. 使用SwiftUI-Introspect,这会改变这个特定 List(我的建议)
  2. 全局更改所有 Lists 的样式(我不建议这样做,因为这可能会影响许多不同的组件)

这些解决方案的结果:

1:内省

struct ContentView: View {
    var body: some View {
        List(0 ..< 5) { item in
            HStack {
                Image(systemName: "clock")

                Text("Placeholder")
            }
        }
        .introspectTableView { tableView in
            tableView.backgroundColor = .clear
        }
        .background(Color.blue)
    }
}

2:全局外观

struct ContentView: View {
    init() {
        UITableView.appearance().backgroundColor = .clear
    }

    var body: some View {
        List(0 ..< 5) { item in
            HStack {
                Image(systemName: "clock")

                Text("Placeholder")
            }
        }
        .background(Color.blue)
    }
}

您也可以使用以下方法来忽略背景颜色的安全区域:

.background(Color.blue.ignoresSafeArea())

【讨论】:

  • 谢谢!除了选项 #2 之外,似乎没有本地方法可以做到这一点。
  • @Robert 不幸的是没有。但是我强烈推荐 Introspect 方法,而且我经常使用它来解决这样的基本问题。内省是比使用UIViewRepresentables 和其他东西更好的方法。
猜你喜欢
  • 2013-02-07
  • 2019-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-30
相关资源
最近更新 更多