1

How do I add a seconds column to the UIDatePicker?

I am currently displaying this:

UIDatePicker

With this UIViewRepresentable in SwiftUI

struct DurationPicker: UIViewRepresentable {
    @Binding var duration: TimeInterval

    func makeUIView(context: Context) -> UIDatePicker {
        let datePicker = UIDatePicker()
        datePicker.datePickerMode = .countDownTimer
        datePicker.addTarget(context.coordinator, action: #selector(Coordinator.updateDuration), for: .valueChanged)
        return datePicker
    }

    func updateUIView(_ datePicker: UIDatePicker, context: Context) {
        datePicker.countDownDuration = duration
    }

    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }

    class Coordinator: NSObject {
        let parent: DurationPicker

        init(_ parent: DurationPicker) {
            self.parent = parent
        }

        @objc func updateDuration(datePicker: UIDatePicker) {
            parent.duration = datePicker.countDownDuration
        }
    }
}

Here is my view code for reference

struct ContentView: View {
   
    @Binding var interval:TimeInterval
    
    init( interval: Binding<TimeInterval> = .constant(0) ) {
      _interval = interval
    }
    
    var body: some View {
        
        NavigationView {
            VStack {
                Text("Hello, world!")
                    .padding()

                DurationPicker(duration: $interval)
                
                NavigationLink(destination: DetailView()) {
                    Text("Show Detail View")
                }
            }
        }
    }
}

I can't figure out how I might go about adding a seconds column, even though the official clock app on iOS displays one.

Witek Bobrowski
  • 3,749
  • 1
  • 20
  • 34
Matt Ward
  • 1,095
  • 1
  • 14
  • 28
  • 1
    Does this answer your question? [UIPickerView that looks like UIDatePicker but with seconds](https://stackoverflow.com/questions/10999575/uipickerview-that-looks-like-uidatepicker-but-with-seconds) – Witek Bobrowski Jan 18 '21 at 20:52
  • It will help point me in the right direction, but if somebody could help me with a modern SwiftUI solution, that would be much appreciated! – Matt Ward Jan 18 '21 at 21:26

0 Answers0