简体   繁体   中英

Change background color of SwiftUI List View

I have a view in SwiftUI and no work a change backgroud color metod. My main code is:

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)
            }
        }
    }

The generic element of my list is a custom element, and the code is (store in another file class):

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)
}

but, unfortunately the background color remains "white" and is not changed. The desidered color is: Color(red: 0.17, green: 0.24, blue: 0.31)

this is a runtime result: simulator

Help me please!

If you can get away with using a LazyVStack or a VStack, you could resolve this easily, but this is just in case you need to use List. Note that this is certainly not production ready code, but a possible solution or a direction as you'll see below.

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) {}
}

Now to use this apply it as a background.

.background(ListBackgroundColorer())

The result:

列表背景着色

Thanks to Asperi for inspiration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM