更改 SwiftUI 列表视图的背景颜色

Posted

技术标签:

【中文标题】更改 SwiftUI 列表视图的背景颜色【英文标题】:Change background color of SwiftUI List View 【发布时间】:2021-03-11 16:00:22 【问题描述】:

我在 SwiftUI 中有一个视图,但没有改变背景颜色的方法。我的主要代码是:

var body: some View 
        ZStack 
            Color(red: 0.17, green: 0.24, blue: 0.31)
                .edgesIgnoringSafeArea(.all)
            VStack
                NavigationLink(destination: WelcomeView(),isActive: $isShowWelcomeView)
                List(Modules.Modules)  module in
                    NavigationLink(destination: ModuleView(moduleName: module.name))
                        ModuleRow(mod: module)
                    
                
                .navigationBarItems(trailing:
                            Button(action: logout()) 
                                Text("Logout")
                            )
                .navigationBarTitle("CORSO")
                .navigationBarBackButtonHidden(true)
            
        
    

我列表的通用元素是自定义元素,代码是(存储在另一个文件类中):

var body: some View 
    ZStack 
        Color(red: 0.17, green: 0.24, blue: 0.31)
                    .cornerRadius(12)
        HStack 
            VStack(alignment: .leading, spacing: 8) 
                Text(mod.name)
                    .font(.title)
                    .fixedSize(horizontal: false, vertical: true)
                    .foregroundColor(.yellow)
                Text(mod.summary)
                    .font(.caption)
                    .foregroundColor(.yellow)
            
            Spacer()
            Image(systemName: mod.imageName)
                .resizable()
                .aspectRatio(contentMode: .fit)
                .frame(width: 50, height: 50, alignment: .center)
                .padding()
                .foregroundColor(.yellow)
        
        .padding()
        .background(Color(red: 0.17, green: 0.24, blue: 0.31))
        .listRowBackground(Color(red: 0.17, green: 0.24, blue: 0.31))
    
    .fixedSize(horizontal: false, vertical: true)
    .shadow(color: Color.black.opacity(0.2), radius: 5, x: 0, y: 2)

但是,不幸的是,背景颜色仍然是“白色”并且没有改变。想要的颜色是:Color(red: 0.17, green: 0.24, blue: 0.31)

这是一个运行时结果: simulator

请帮帮我!

【问题讨论】:

【参考方案1】:

如果您可以不使用 LazyVStack 或 VStack,则可以轻松解决此问题,但这只是以防万一您需要使用 List。 请注意,这肯定不是生产就绪代码,而是一个可能的解决方案或方向,如下所示。

struct ListBackgroundColorer: UIViewRepresentable 
    public func makeUIView(context: Context) -> UIView 
        
        let view = UIView()
        DispatchQueue.main.async 
            view.superview?.superview?.superview?.superview?.superview?.subviews.first(where: NSStringFromClass(type(of: $0)) == "_UISystemBackgroundView")?.subviews[0].backgroundColor = UIColor(red: 0.5, green: 0.5, blue: 0.5, alpha: 1.0)
        
        return view
    
    
    public func updateUIView(_ uiView: UIView, context: Context) 

现在使用它作为背景。

.background(ListBackgroundColorer())

结果:

感谢 Asperi 的启发。

【讨论】:

以上是关于更改 SwiftUI 列表视图的背景颜色的主要内容,如果未能解决你的问题,请参考以下文章

SwiftUI 列表颜色背景

SwiftUI:如何更改 NavigationView 的色调(背景颜色)?

更改选项卡式视图 SwiftUI 的凹槽区域背景颜色

SwiftUI 如何根据获取的数据更改视图的背景颜色?

在 SwiftUI 中更改 UIView 背景颜色

更改选项卡式视图栏颜色 SwiftUI