Search code examples
swiftswiftuidatepickeruidatepicker

How to change Selection colour(AM,PM) and time of GraphicalDatePickerStyle Date picker Swiftui


How to Change GraphicalDatePickerStyle Date picker text color and Selection Colour:-

Code:-

 DatePicker("Time",selection: dateProxy,displayedComponents: [.hourAndMinute])
    .datePickerStyle(GraphicalDatePickerStyle())
    .accentColor(.white)
    .labelsHidden()

Output:-

enter image description here

But I want to achieve DatePicker given below, is it possible in GraphicalDatePickerStyle Style or not?

Try to Achieve:-

enter image description here


Solution

  • Seems like you are looking for the dark mode picker! Just apply the dark colorScheme to the component:

    DatePicker("Time", selection: $date, displayedComponents: [.hourAndMinute])
        .datePickerStyle(GraphicalDatePickerStyle())
        .background(Color.black)
        .environment(\.colorScheme, .dark) // <- This modifier
    

    Result:

    Result


    🌈 Apply color

    Also, you can apply the colorMultiply modifier for coloring the picker in a pleasant way:

    Colors


    Demo Full Code

    struct ContentView: View {
    
        @State private var date = Date()
    
        var colors: [Color] = [.white, .gray, .red, .green, .blue, .orange, .yellow, .pink, .purple] // <- You can use any color! This is just for demonstrate
    
        var body: some View {
            VStack {
                ForEach(colors, id:\.self) { color in
                    DatePicker("Time", selection: $date, displayedComponents: [.hourAndMinute])
                        .datePickerStyle(GraphicalDatePickerStyle())
                        .background(Color.black.grayscale(1))
                        .colorMultiply(color) // <- This line does the trick
                        .environment(\.colorScheme, .dark) // <- Don't Forget this
                }
            }
        }
    }