Search code examples
swiftswiftuiuidatepicker

How can I set countDownTimer mode in DatePicker on SwiftUI?


With UIKit, the UIDatePicker allow set mode to UIDatePicker.Mode.countDownTimer and we can have set duration.

With SwiftUI, I don't see any native way to solve this. The only way would be doing interface with UIKit?

This is how I need

[UPDATE]
Only solution found

DurationPickerView.swift

import SwiftUI
import UIKit

struct DurationPickerView: UIViewRepresentable {
    @Binding var time: Time

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

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

    func updateUIView(_ datePicker: UIDatePicker, context: Context) {
        let date = Calendar.current.date(bySettingHour: time.hour, minute: time.minute, second: time.second, of: datePicker.date)!
        datePicker.setDate(date, animated: true)
    }

    class Coordinator: NSObject {
        var durationPicker: DurationPickerView

        init(_ durationPicker: DurationPickerView) {
            self.durationPicker = durationPicker
        }

        @objc func onDateChanged(sender: UIDatePicker) {
            print(sender.date)
            let calendar = Calendar.current
            let date = sender.date
            durationPicker.time = Time(hour: calendar.component(.hour, from: date), minute: calendar.component(.minute, from: date), second: calendar.component(.second, from: date))
        }
    }
}

Time.swift

import Foundation

struct Time {
    var hour: Int
    var minute: Int
    var second: Int = 0
}

Solution

  • The only way to get the countdown behavior right now is by wrapping UIDatePicker in a custom view.

    Here is a simplified version of Ailton Vieira Pinto Filho's code using countDownDuration.

    import 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
            }
        }
    }