【问题标题】:Why background color of List is different while presenting view in SwiftUI?为什么在 SwiftUI 中显示视图时 List 的背景颜色不同?
【发布时间】:2020-08-10 18:36:41
【问题描述】:

我正在呈现视图 (AddItemView) 中实现列表。我希望任何视图中的背景颜色都与 List 相同。

struct HomeView: View {
    @State private var showAddItemView: Bool = false
    var body: some View {
        NavigationView {
            List(0..<9, id: \.self) { i in
                Text("Row \(i)")
            }
            .navigationTitle("Home")
            .navigationBarItems(trailing:
                Button("Add") {
                    showAddItemView.toggle()
                })
            .sheet(isPresented: $showAddItemView) {
                AddItemView()
            }    
        }
    }
}

struct AddItemView: View {
    init(){
        UITableView.appearance().backgroundColor = .clear
    }
    var body: some View {
        NavigationView {
            List(0..<9, id: \.self) { i in
                Text("Row \(i)")
            }.background(Color(UIColor.systemGroupedBackground))
            .listStyle(InsetGroupedListStyle())
            .navigationBarTitle("Add Item View", displayMode: .inline)    
        }
    }
}

上面的代码是用 InsetGroupedListStyle 创建简单的列表。但是呈现视图时背景颜色不同(在我的情况下为 AddItemView)。

我已经尝试过https://stackoverflow.com/a/58427518/7084910

如何在呈现的视图中设置列表的背景颜色,就像在任何普通列表中一样。 Red/Yellow/Green 可以设置为 List,“但是”我想要与 HomeView 中的普通列表相同,可以在明暗模式下工作。

【问题讨论】:

  • 什么是呈现视图?你会显示代码吗?
  • @Asperi,我已经更新了问题

标签: ios swift swiftui


【解决方案1】:

使用这个:

var body: some View {
    NavigationView {
        List(0..<9, id: \.self) { i in
            Text("Row \(i)")
        }
        .colorMultiply(Color.red)

    }
}

【讨论】:

  • Red、Yellow、Green 等正在使用 init() { .clear }。但我想要与 HomeView 中的相同。
  • 选择与 rgb 值相同的颜色并使用它。你还在用 ans 遇到什么问题?
  • 适用于明暗模式。 systemGroupedBackground 非常适合两者。我想要没有配色方案的条件。
【解决方案2】:

他们认为这是.sheet 更好的视觉表示(可能是为了使其更可确定)...

SwiftUI 2.0

.fullScreenCover 提供您想要的。另一种方法是使用一些转换手动呈现AddItemView

.navigationBarItems(trailing:
    Button("Add") {
        showAddItemView.toggle()
    })
.fullScreenCover(isPresented: $showAddItemView) {
    AddItemView()
}

【讨论】:

  • 感谢您的解决方案。但就我而言,HomeView 已经是 fullScreenCover。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-17
  • 2022-01-03
  • 1970-01-01
  • 2021-09-09
  • 2020-05-15
  • 2020-03-02
  • 1970-01-01
相关资源
最近更新 更多