What's the default color for the background of a SwiftUI List?
Asked Answered
P

6

9

I know how to change the background color of a SwiftUI Views list, but I cannot find the default color. I've tried using the MacOS 'Digital Color Meter', but it just doesn't pick it up right.

As you can see in this image, I've tried to set the background color of a list row (using .listRowBackground to the exact same of the surrounding list based off of the values from Digital Color Meter.

Does anyone actually know what the default background color actually is?

Image

Psychrometer answered 8/12, 2020 at 8:25 Comment(1)
Is it possible that you got RGB values wrong? Maybe you have used an external screen with different Color Space? Please check my answer for the correct values.Richmound
R
7

Short answer: it looks like it is UIColor.secondarySystemBackground

Long answer:

I have tried Digital Color Meter app the sam as you did. It shows this RGB values: 242, 242, 247.

I have created such color: enter image description here

Here is my code:

import SwiftUI

extension Color {
    public static let ListBGColor = Color("ListBGColor")
}

struct ContentView: View {
    var body: some View {
        List {
                ForEach(0..<5) {_ in
                    Text("Hello, world!")
                        .padding()
                }
                .listRowBackground(Color.ListBGColor)
        }
        .listStyle(InsetGroupedListStyle())
    }
}

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

Result: the same color as the background (testes on Simulator and iPhone): enter image description here

Richmound answered 14/7, 2021 at 8:4 Comment(0)
P
8

After doing some testing, it looks like it might be UIColor.secondarySystemGroupedBackground.

Examples

Default

Without setting secondary system color

With UIColor.secondarySystemGroupedBackground

I'm using it to set the list row background: .listRowBackground(Color(UIColor.secondarySystemGroupedBackground))

Using the secondary system color

Postrider answered 3/9, 2023 at 17:11 Comment(0)
R
7

Short answer: it looks like it is UIColor.secondarySystemBackground

Long answer:

I have tried Digital Color Meter app the sam as you did. It shows this RGB values: 242, 242, 247.

I have created such color: enter image description here

Here is my code:

import SwiftUI

extension Color {
    public static let ListBGColor = Color("ListBGColor")
}

struct ContentView: View {
    var body: some View {
        List {
                ForEach(0..<5) {_ in
                    Text("Hello, world!")
                        .padding()
                }
                .listRowBackground(Color.ListBGColor)
        }
        .listStyle(InsetGroupedListStyle())
    }
}

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

Result: the same color as the background (testes on Simulator and iPhone): enter image description here

Richmound answered 14/7, 2021 at 8:4 Comment(0)
D
6

If you're talking about List{ }, it is .systemGroupedBackground. Check in dark mode: .systemSecondaryBackground does something different.

Dismal answered 21/11, 2022 at 13:58 Comment(0)
C
1

2024, iOS 17:

The default list background color is simply:

.background.secondary

For example:

VStack {
   Text("Color me gray!")
}
.background(.background.secondary)

... will give the VStack the same background color as a List.

This will also work (if you need to support older iOS versions:

VStack {
       Text("Color me gray!")
    }
.background(Color(UIColor.secondarySystemGroupedBackground))

Some more insight on this can be found here.

Celiotomy answered 11/7 at 21:5 Comment(0)
B
0

The default color is different for each ListStyle and platform and I don't think SwiftUI exposes these colors. You could technically Introspect the list view and get it's background color through UIKit.

Ba answered 8/12, 2020 at 8:30 Comment(0)
N
0
VStack {
    Text("The background same as list, it also support dark mode")
}
.background(
    Color(.systemGroupedBackground)
)
Nationalist answered 9/9 at 15:13 Comment(1)
Thank you for your interest in contributing to the Stack Overflow community. This question already has a number of answers—including a few that have been validated by the community. Are you certain your approach hasn’t been given previously? If so, it would be useful to explain how your approach is different, under what circumstances your approach might be preferred, and/or why you think the previous answers aren’t sufficient. Can you kindly edit your answer to offer an explanation?Agan

© 2022 - 2024 — McMap. All rights reserved.