【问题标题】:How can I change the background color in SwiftUI when I am using NavigationView and List?使用 NavigationView 和 List 时如何更改 SwiftUI 中的背景颜色?
【发布时间】:2019-10-31 14:04:00
【问题描述】:

我想更改背景颜色,但我正在使用 NavigationView 和 List。这就是为什么 ZStack 方法对我不起作用的原因。

这是我的代码:

NavigationView {
    List {
        Text("Some Text")
    }

    .navigationBarTitle("Test")
}

【问题讨论】:

    标签: ios swift swiftui


    【解决方案1】:

    您可以在列表中使用 .listRowBackground。

      ZStack{
            Color.blue
       NavigationView {
        ZStack{
            Color.red
           List {
            ZStack{
                Color.green
                Text("Some Text").background(Color.green).opacity(0.5)
    
            }.listRowBackground(Color.red)
            }
        }
           .navigationBarTitle("Test")
       }.padding()
    
        }
    

    【讨论】:

    • 它不太好用。导航栏是固定的。所以较大的列表滚动浏览看起来很奇怪的导航栏。不过还是谢谢你的回答
    • 您可以使用 .inline 样式使导航栏更小。原则上,如果您知道如何自定义列表,则可以使用 listStyle 配置列表。
    • 您可能需要查看这篇文章:stackoverflow.com/questions/57128547/…
    【解决方案2】:
    NavigationView {
        List {
             Text("Some Text")
        }
        .navigationBarTitle("Test")
    }.colorMultiply(Color.green)
    

    【讨论】:

    • 但是如果我想使用图像,那么它们也会被着色
    【解决方案3】:

    我终于找到了问题的答案:

    init() {
        UITableView.appearance().backgroundColor = UIColor(named: "CustomBackgroundColor")
        UITableViewCell.appearance().backgroundColor = UIColor(named: "CustomBackgroundColor")
        UITableView.appearance().tableFooterView = UIView()
    }
    
    var body: some View {
        NavigationView {
            List {
                Text("Hello")
            }
    
            .navigationBarTitle("Test")
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-01
      • 2020-10-11
      • 2019-11-17
      • 1970-01-01
      • 2020-05-15
      • 1970-01-01
      • 2019-10-24
      • 2019-12-21
      相关资源
      最近更新 更多