I want to change the background color of the cell.

Asked 1 years ago, Updated 1 years ago, 430 views

struct ContentView:View { 
    @State var color=false
    varbody:someView {
        List {…}.listBackground (color?.white:.black)
        Toggle ("text", isOn:$color)
    }
}

does not change the color.
I wonder if the writing method is different.

Also, it does not change to .listRowBackground(color?white:.black)).
scrollContentBackground to hide does not change.

Can't I change the color of the cell?

swift swiftui

2022-12-28 22:11

1 Answers

.listRowBackground() should be attached to each View cell, not to the entire List.

import SwiftUI

struct ContentView:View {
    
    @State var isBasicColor=true
    
    varbody:someView {
        List{ 
            Text ("Blue")
                .listRowBackground (isBasicColor?Color.blue:.red)
            Text ("Red")
                .listRowBackground (isBasicColor?Color.red:.white)
            Text ("Yellow")
                .listRowBackground (isBasicColor?Color.yellow:.blue)
        }
        .padding()
        Toggle ("Cell colors", isOn: $isBasicColor)
    }
}

Cell Color


2022-12-29 02:53

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.