Reputation: 772
I saw this question but it doesn't work in SwiftUI.
I tried .forgroundColor
and .accentColor
but they don't change the texts' color.
Upvotes: 21
Views: 21489
Reputation: 20965
the best and most robust solution is to use an overlay on the button and than there are no limitations
DatePicker("", selection: $toDate, displayedComponents: .date)
.datePickerStyle(.compact)
.labelsHidden()
.accentColor(.jPrimary)
.overlay {
ZStack {
Color.jBackground
.frame(maxWidth: .infinity, maxHeight: .infinity)
.cornerRadius(4)
Text(formattedDate(toDate))
.foregroundColor(.jPrimary)
}
.allowsHitTesting(false)
}
Upvotes: 1
Reputation: 554
.accentColor() will be deprecated in the near future.
.colorInvert() will stop working as soon as tapped.
For now im using:
.preferredColorScheme(.dark)
.tint(baby.color.bgColor)
.colorMultiply(baby.color.bgColor)
But using .preferredColorScheme(.dark) in some cases affects all the view tones depending on the colors you are using.
Upvotes: 1
Reputation: 348
I found applying this modifier to the DatePicker
to be cleaner:
Forces the color to white
.environment(\.colorScheme, .dark)
Forces the color to dark
.environment(\.colorScheme, .light)
Upvotes: 2
Reputation: 79
you can do this to make the text appear white in light or dark mode if you use a background color
struct PickerDate: View {
@Environment(\.colorScheme) var colorScheme
@State var date: Date
var body: some View {
VStack {
DatePicker("Record Date", selection: $date, in: ...Date(), displayedComponents: .date)
.labelsHidden()
.colorMultiply(colorScheme == .dark ? .black : .white)
.colorInvert()
}
}
}
}
Upvotes: 5
Reputation: 8091
try this:
var body: some View {
Group {
DatePicker(selection: $selected) {
Text("Date")
}
.colorInvert()
.colorMultiply(Color.blue)
}
}
Upvotes: 16
Reputation: 41
I use extension for customized text color for both of light mode and dark mode.
extension View {
@ViewBuilder func changeTextColor(_ color: Color) -> some View {
if UITraitCollection.current.userInterfaceStyle == .light {
self.colorInvert().colorMultiply(color)
} else {
self.colorMultiply(color)
}
}
}
and sample is as below.
DatePicker("Date", selection: $selection)
.labelsHidden()
.changeTextColor(.green)
Upvotes: 4
Reputation: 420
I just simple set the accentColor and it works.
@State private var date = Date()
DatePicker("Select a date",
selection: $date, in: ...Date(),
displayedComponents: .date)
.labelsHidden()
.accentColor(.orange)
Upvotes: 15
Reputation: 7736
Using .colorInvert()
and .colorMultiply(.white)
will change the text color to white but won't work when the device is in Dark Mode. Instead, try this:
DatePicker("Date", selection: $selection)
.colorScheme(.dark) // or .light to get black text
Upvotes: 22
Reputation: 1
DatePicker("DatePicker", selection: self.$date, displayedComponents: .hourAndMinute)
.labelsHidden()
.colorMultiply(Color.white)
.colorInvert()
Upvotes: 0