Reputation: 271
I am using datepicker when user select current date not future date and not scrolling date picker in swift 2.0
override func viewDidLoad() {
let datePickerView:UIDatePicker = UIDatePicker()
datePickerView.datePickerMode = UIDatePickerMode.Date
sender.inputView = datePickerView
//datePickerView.minimumDate = datePickerView.date
datePickerView.maximumDate = datePickerView.date
datePickerView.addTarget(self, action: #selector(BookAppointmentView.datePickerValueChanged(_:)), forControlEvents: UIControlEvents.ValueChanged)
func datePickerValueChanged(sender:UIDatePicker) {
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
DatePickerField.text = dateFormatter.stringFromDate(sender.date)
}
Upvotes: 2
Views: 5080
Reputation: 213
Use this maximumdate property of datepicker. The maximumdate will be always the current date.
let DatePicker = UIDatePicker()
DatePicker.maximumDate = NSDate()
Edit You are doing it wrong.
// DatePicker.maximumDate = DatePicker.date //This is a wrong approach. It will not allow you from scrolling
DatePicker.maximumDate = NSDate() // This will avoid you from setting a future date.
Upvotes: 4
Reputation: 93
it is so simple. just maximumDate property in nsdatepicker as today date
self.datePicker.maximumDate = NSDate()
Upvotes: 1
Reputation: 1949
Try the following :
change datePickerView.maximumDate to NSDate.date
It wont allow to select future date. ie., maximum date you can select is today.
Hope it helps..
Upvotes: 1