article

Monday, May 17, 2021

SwiftUI Background Color list with alternate colors

SwiftUI Background Color list with alternate colors
 
//
//  ContentView.swift
//  Testapp
//
//  Created by Cairocoders
//

import SwiftUI

struct ContentView: View {
    
    @State private var items = ["One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten"]

    var body: some View {
        List {
            ForEach(items.indices) { index in
                Text(items[index])
                    .listRowBackground((index  % 2 == 0) ? Color(.systemBlue) : Color(.yellow))
            }
        }
    }
}

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

Related Post