Since Dark mode was introduced in iOS, the easiest was to define colors in Xcode/Assets. This way our app will change the colors automatically.
I have these three colors for my project:
Define them in Assets:
Using AccentColor
is easy, you need to type only:
.foregroundColor(Color.accentColor)
but for pastelGreen, you need to type:
.foregroundColor(Color("pastelGreen"))
What if there is an eaier way and you can use your owned defined colors as SwiftUI built-in Color
.
Create a new file ColorExtension.swift
and add the following:
import SwiftUI
extension Color {
static let pastelGreen = Color("pastelGreen")
}
Now, you can use your defined color as:
.foregroundColor(Color.pastelGreen)