Reputation: 77
I want to use date picker with the empty option. Like if user don't want to select day, month or year there will be an option of "-"(empty).
Here is an image for better understanding
Upvotes: 1
Views: 2183
Reputation: 1436
You can't change the values of UIDatePicker. If you want custom values to be displayed on rows, you can use UIPickerView. Sample code for showing months along with "-" is:
let months: [String] = ["-" ,"January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December"]
let pickerView: UIPickerView = UIPickerView()
pickerView.delegate = self
pickerView.dataSource = self
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return months.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return months[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
// do whatever you want when specific row is selected
}
Upvotes: 4
Reputation: 402
For such way you need to use not DATE PICKER VIEW for this way you can use Picker View, and manually setup date.
Ex:
var monthsArray = ["-", "May", "June", "...", "December"]
var yearsArray = ["-", "2018", "2017", "2016", "...."]
var daysArray = ["1", "2", "-", "3", "4"]
Upvotes: 1