SwiftDatePicker is a custom date picker implementation for iOS. It provides a visually appealing and reusable DatePickerView
component that can be easily integrated into any iOS project. The date picker allows users to select a date, and its delegate methods enable handling actions like confirmation and cancellation.
- Customizable
UIDatePicker
with rounded background view. - Delegate methods to handle
OK
andCancel
button actions. - Dynamically adds and removes the date picker view.
- Supports minimum date selection starting from today.
- Provides formatted date strings and logical day identifiers (e.g., "today", "yesterday", "tomorrow").
To integrate SwiftDatePicker into your project:
- Clone the repository or download the source code.
- Copy the
DatePickerView.xib
andDatePickerView.swift
files into your project. - Link the
DatePickerView
class with the corresponding.xib
file.
In your UIViewController
:
@IBAction func datePickerButtonAction(_ sender: UIButton) {
self.view.endEditing(true)
self.addDatePickerView()
}
func addDatePickerView() {
let datePickerView = DatePickerView(frame: self.view.bounds)
datePickerView.tag = 1001
datePickerView.delegate = self
datePickerView.datePicker.date = Date()
datePickerView.datePicker.minimumDate = Date()
self.view.addSubview(datePickerView)
self.view.bringSubviewToFront(datePickerView)
}
Conform to the DatePickerViewDelegate
protocol to handle the button actions:
extension ViewController: DatePickerViewDelegate {
func datePickerOkButtonAction(view: DatePickerView, sender: UIButton, date: Date) {
// Handle selected date
print("Selected Date:", date)
self.removeSubView()
}
func datePickerCancelButtonAction(view: DatePickerView, sender: UIButton) {
// Handle cancellation
self.removeSubView()
}
}
func removeSubView() {
for subview in self.view.subviews {
if subview.tag == 1001 {
subview.removeFromSuperview()
}
}
}
You can easily modify the appearance of the DatePickerView
:
- Change the background color of
datePickerBackgroundView
for a different look. - Modify the corner radius for the
datePickerBackgroundView
in thesetDatePickerUI()
method. - Add more UI elements to the
DatePickerView.xib
file if needed.
- iOS 13.0+
- Swift 5
- Xcode 12 or higher
Contributions are welcome! Please fork the repository and submit a pull request for any enhancements or bug fixes.
This project is licensed under the MIT License. See the LICENSE
file for details.
Developed by Jaydeep Godhani. If you have any questions or feedback, feel free to reach out!
- Thanks to the iOS community for their continuous support and inspiration!