Search code examples
swiftenumsuidatepickerswift-extensions

Is it possible to write a Swift Extension for UIDatePicker.Mode


From the Apple documentation, UIDatePicker.Mode is an enum of type Int with four cases.

enum Mode : Int

case time
case date
case dateAndTime
case countDownTimer

I am curious to know whether I can add a fifth case monthYear (= 4) to enable me to create a custom implementation of UIDatePicker.

The purpose of this fifth case would be to display only the month and year rollers in the picker control.

(Please note that I am aware that I could - and probable will - create a custom UIPickerView to handle this, however I am curious to know whether I can gracefully extend UIDatePicker class or UIDatePicker.Mode enum to achieve this.)

I've done some reading on writing an extension for an enum. I've read some interesting articles and reviewed a few Stack Overflow Q&A that include functioning workarounds...

So in my humble opinion, to be able to neatly extend UIDatePicker, I'd need to first be able to extend UIDatePicker.Mode.

I've also read in the Apple documentation under the heading Appearance...

The appearance of UIDatePicker is not customizable.

This seems fairly definitive to me, however it does not hurt to ask the question...

Is it possible to write a Swift Extension for UIDatePicker.Mode so that I can add a fifth case monthYear and for UIDatePicker so that I can write a custom implementation that shows just month and year rollers in the control?


Solution

  • No, you can not modify the functionality of UIDatePicker to support your own custom mode.

    You either need to implement your own custom date picker using UIPickerView or find a 3rd party component that has already done so.