christophriepe
christophriepe

Reputation: 1663

How to chose Color depending on System Background Color in Swift 5?

I am currently working with SwiftUI Colors.

Problem: I have a Card Component which has a Background Color of systemGray6. This works fine for most Views (all Views with systemBackground Background Color).

However, I have some Modal Views which come with tertiarySystemBackground. This doesn't change anything in LightMode, but changes the Background Color in DarkMode to the same as systemGray6. As a result, the Background and the Card have the same Color and the Card is not visible anymore.

My Solution so far:

struct Card: View {
    var isInsideModal: Bool = false

    var body: some View {
        // Some Content
        .background(isInsideModal ? Color.systemGray6 : Color.systemGray5) // I added this in an Extension of Color
    }
}

Question: Is there any Possibility of automating this Behavior without always having to explicitly pass the desired Background Color to the Card View?

Thanks for your help.

Upvotes: 0

Views: 330

Answers (1)

Jeff Cournoyer
Jeff Cournoyer

Reputation: 504

You could create Color Sets in your xcassets folder. It provides you with the ability to choose different Appearances depending on whether your app is operating under dark or light mode.

Upvotes: 2

Related Questions