How do I modify the background color of a List in SwiftUI?

后端 未结 14 1281
醉酒成梦
醉酒成梦 2020-12-03 02:33

I\'m trying to recreate an UI I built with UIKit in SwiftUI but I\'m running into some minor issues.

I want the change the color of the List here, but n

相关标签:
14条回答
  • 2020-12-03 03:06

    There is an argument: listRowBackground() in SwiftUI, but if you use List directly to iterate the data collection, it doesn't work.

    Here is my workaround:

        List {
            // To make the background transparent, we have we use a ForEach as a wrapper
            ForEach(files) {file in
                Label(
                    title: { Text(file.name ?? fileOptionalFiller).lineLimit(listRowTextLineLimit) },
                    icon: { AppIcon.doc.foregroundColor(.primary) }
                )
            }
            .listRowBackground(Color.primary.colorInvert())
        }
    

    Basically, listRowBackground() works if you use a ForEach inside List.

    0 讨论(0)
  • 2020-12-03 03:08

    Someone may find this useful if attempting to create a floating type cell with SwiftUI using .listRowBackground and applying .padding

    var body: some View {
        NavigationView {
            List {
                ForEach (site) { item in
                    HStack {
                        Text(String(item.id))
    
                        VStack(alignment: .leading) {
                            Text(item.name)
                            Text(item.crop[0])
                        }
    
                    }.listRowBackground(Color.yellow)
                          .padding(.trailing, 5)
                          .padding(.leading, 5)
                          .padding(.top, 2)
                          .padding(.bottom, 2))
                }
            }
                .navigationBarTitle(Text("Locations"))
        }
    }
    
    0 讨论(0)
  • 2020-12-03 03:09

    struct ContentView: View {
    
        var strings = ["a", "b"]
    
        var body: some View {
    
            List {
                ForEach(strings, id: \.self) { string in
                    Text(string)
                }.listRowBackground(Color.green)
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-03 03:10

    This will set the background of the whole list to green:

    init() {
       UITableView.appearance().separatorStyle = .none
       UITableViewCell.appearance().backgroundColor = .green
       UITableView.appearance().backgroundColor = .green
    }
    
    0 讨论(0)
  • 2020-12-03 03:10

    .colorMultiply(...)

    For the List background use .colorMultiply(Color.yourColor) modifier

    Example:

    List (elements, id:\.self ) { element in
    
         Text(element)
    
    }
    .colorMultiply(Color.red) <--------- replace with your color
    

    0 讨论(0)
  • 2020-12-03 03:11

    I do not know what is the connection but if you wrap the list with Form it is working.

    Form {
         List(viewModel.currencyList, id: \.self) { currency in
            ItemView(item: currency)
         }
          .listRowBackground(Color("Primary"))
          .background(Color("Primary"))
    }
    
    0 讨论(0)
提交回复
热议问题