在 SwiftUI 中修改 List 的背景颜色?

Modify the background colour of a List in SwiftUI?

我想在 SwiftUI 中将背景白色更改为另一种颜色。

struct ListCustom: View {
var body: some View {
    ContentView1()
    }
}

struct ContentView1: View {
@State var items = Array(1...20)

init() {
    UITableView.appearance().backgroundColor =  UIColor.init(.red)
    UITableViewCell.appearance().backgroundColor =  UIColor.init(.red)
    
    let color = UIView()
    color.backgroundColor = UIColor.clear
    UITableViewCell.appearance().selectedBackgroundView = color
}

var idetifca = UUID()
var body: some View {
    VStack {
        Button("Shuffle") {
            self.items.shuffle()
        }

        List(items, id: \.self) {_ in
            Screen(Array1: $items).listRowBackground(Color.green)
        }.id(idetifca).listStyle(PlainListStyle()).background(Color.blue)
            .listRowBackground(Color.blue)
    }
}
}


struct Screen: View {

@Binding var Array1 : [Int]

var body: some View {
    Group {
        if self.Array1.count > 0{
            SectionTitleUI(titleStrng: "Testing", rightTitleString: "", countShow:  0) {}
            ForEach(self.Array1.indices, id: \.self) { notification in
                NotificationView(notificationInfo: self.Array1[notification])
                    .listRowBackground(Color.red)
            }
        }
    }
    .navigationBarHidden(false)
}
}

import SwiftUI

struct NotificationView: View {

@State var notificationInfo = 0

var body: some View {
    VStack {
        HStack(alignment:.top) {
                VStack(alignment: .leading, spacing: 10){
                    HStack(alignment:.top){
                        Text("Text").onAppear{
                            print("rrrr")
                        }
                            .foregroundColor(.black)
                    }
                    .fixedSize(horizontal: false, vertical: true)
                    Group{
                        Text("Text111")
                    }
                    .font(.system(size: 15.0))
                    .foregroundColor(.gray)
                }
                .frame(maxWidth: .infinity ,alignment: .topLeading)
            
        }
        .padding(.all)
        Divider()
            .background(Color.white)
            .offset(x: 0, y:0)
    }.background(notificationInfo == 0 ? Color.red : Color.clear).listRowBackground(Color.red)
   }
}


struct SectionTitleUI: View {
var titleStrng = "2"
var rightTitleString = "3"
var countShow = 4
var comeFromPublicRide = false
var changeFontSize = false
var action: (() -> Void)?

var body: some View {
    VStack(alignment: .leading) {
        HStack {
            Text(titleStrng).font(.system(size:changeFontSize == true ? 15 : 18, weight: .bold, design: .default))
                .foregroundColor(.white)
                .padding(.leading)
                .fixedSize(horizontal: false, vertical: true)
            if countShow != 0{
                Text("\(countShow)").font(.system(size: 16, weight: .bold, design: .default))
                    .foregroundColor(.gray)
                    .padding(.leading,0)
            }
            Spacer()
        }.padding(5)
        .frame(height: 45)
        .background(Color.red)
    }.listRowBackground(Color.red)
}
}

输出:-

当用户运行应用程序的列表颜色未在所有单元格中完美设置时,自动显示白色。当用户滚动列表时,然后自动更改我设置的颜色。

问题:-如何一次性改变整个列表的颜色?

谁能告诉我如何更改列表颜色,我已经尝试使用上面的代码但还没有结果。

如有任何帮助,我们将不胜感激。

提前致谢。

删除 ContentView1init 解决了问题。

此问题是由您的代码设置 selectedBackgroundView 引起的,但您不需要任何设置 init,因为它仍会按您的预期运行。

给你!您只需在各处设置列表行背景颜色...我认为您因此而感到困惑。

您只需在 Screen 结构中设置列表行的背景颜色。希望你明白我改变了什么。

import SwiftUI

struct ContentView: View {
    @State var items = Array(1...20)
    
    var idetifca = UUID()
    
    var body: some View {
        VStack {
            Button("Shuffle") {
                self.items.shuffle()
            }
            
            List(items, id: \.self) {_ in
                Screen(Array1: $items)
            }
            .id(idetifca)
            .listStyle(PlainListStyle())
        }
    }
}

struct Screen: View {
    @Binding var Array1 : [Int]
    
    var body: some View {
        Group {
            if self.Array1.count > 0{
                SectionTitleUI(titleStrng: "Testing", rightTitleString: "", countShow:  0) {}
                .listRowBackground(Color.red)
                ForEach(self.Array1.indices, id: \.self) { notification in
                    NotificationView(notificationInfo: self.Array1[notification])
                }
                .listRowBackground(Color.red)
            }
        }
        .navigationBarHidden(false)
    }
}

struct NotificationView: View {
    @State var notificationInfo = 0
    
    var body: some View {
        VStack {
            HStack(alignment:.top) {
                VStack(alignment: .leading, spacing: 10){
                    HStack(alignment:.top){
                        Text("Text").onAppear{
                            print("rrrr")
                        }
                        .foregroundColor(.black)
                    }
                    .fixedSize(horizontal: false, vertical: true)
                    Group{
                        Text("Text111")
                    }
                    .font(.system(size: 15.0))
                    .foregroundColor(.gray)
                }
                .frame(maxWidth: .infinity ,alignment: .topLeading)
                
            }
            .padding(.all)
            Divider()
                .background(Color.white)
                .offset(x: 0, y:0)
        }
        .background(notificationInfo == 0 ? Color.red : Color.clear)
    }
}

struct SectionTitleUI: View {
    var titleStrng = "2"
    var rightTitleString = "3"
    var countShow = 4
    var comeFromPublicRide = false
    var changeFontSize = false
    var action: (() -> Void)?
    
    var body: some View {
        VStack(alignment: .leading) {
            HStack {
                Text(titleStrng)
                    .font(.system(size:changeFontSize == true ? 15 : 18, weight: .bold, design: .default))
                    .foregroundColor(.white)
                    .padding(.leading)
                    .fixedSize(horizontal: false, vertical: true)
                if countShow != 0{
                    Text("\(countShow)")
                        .font(.system(size: 16, weight: .bold, design: .default))
                        .foregroundColor(.gray)
                        .padding(.leading,0)
                }
                Spacer()
            }
            .padding(5)
            .frame(height: 45)
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

有什么需要问我