A beautiful UIView wrapper for UIDatePicker. Requires iOS 13.4 or newer
- Storyboard
Create a UIView
and set its custom class to RSDatePicker
and link it with a IBOutlet
in the class you want
- Code
let datePicker = RSDatePicker(frame: CGRect)
You can customize the date picker in multiple ways:
currentDate
sets the default date (optional)initialText
sets the default text if a date is not selected (optional)minimumDate
sets the minimum selectable date (optional)maximumDate
sets the maximum selectable date (optional)pickerMode
sets the date picker mode (.date
ortime
) (optional)dateFormat
sets the date format for the visible label (optional)closeWhenSelectingDate
allows you to enable/disable closing after a date was pickedcloseAnimationDuration
controls the animation speed when closing the pickercalendarIconIsHidden
controls if the calendar icon is hiddencalendarIconSizeMultiplier
controls the multiplier of the aspect ratio of the calendar imageviewcalendarIconImage
sets the calendar iconleft/right/top/bottomMargin
changes the margins of the container view
There is also a callback for receiving the new picked date: didChangeDate
You can also get the current date whenever you need by using currentDate
Note
While running on iPad, it's recommended to use forceScalePicker = true
so the picker will scale to the full width.
There is also the pickerAlignment
property which can change the date picker modal position
import RSDatePicker
(...)
@IBOutlet weak var datePicker: RSDatePicker!
(...)
self.datePicker.cornerRadius = 16
self.datePicker.leftMargin = 20
self.datePicker.rightMargin = 16
self.datePicker.pickerMode = .date
self.datePicker.currentDate = Calendar.current.date(byAdding: .day, value: -1, to: Date()) // yesterday
self.datePicker.initialText = "Select Date"
self.datePicker.calendarIconIsHidden = false
self.datePicker.calendarIconTint = .label
self.datePicker.calendarIconSizeMultiplier = 0.8
self.datePicker.calendarIconImage = UIImage(systemName: "calendar")!
self.datePicker.forceScalePicker = true
self.datePicker.pickerAlignment = .center
self.datePicker.closeWhenSelectingDate = true
self.datePicker.minimumDate = Calendar.current.date(byAdding: .day, value: -4, to: Date()) // 4 days ago
self.datePicker.maximumDate = Calendar.current.date(byAdding: .day, value: 20, to: Date()) // 20 days in the future
self.datePicker.didChangeDate = { newDate in
print(newDate)
}
RSDatePicker is available under the MPL-2.0 license. More info available here.