7

I want to design a UI for setting date of birth. For that I am choosing UIDatePickerView. Now I want to restrict the dates to between 01-01-1990 and current date. It should not show future dates and past dates. Currently I am using the following code:

NSDateComponents *components=[[NSDateComponents alloc] init];

[components setYear:1900];

pickerView.datePicker.minimumDate = [[NSCalendar currentCalendar] dateFromComponents:components];
pickerView.datePicker.maximumDate=[NSDate date]

It is restricting the dates, but it is allowing the user to scroll to the future dates as well as past dates. How can I hide or avoid them?

I am able to see that feature in the contacts app on the iPad. To see go to Contacts --> press the "+" symbol on top, then press on add birthday button.

shim
  • 9,289
  • 12
  • 69
  • 108
MuraliMohan
  • 1,063
  • 1
  • 11
  • 30

2 Answers2

6

it scroll but visible on gray color, but it is not selectable, it is not possible to stop on scroll

To Disable Past date :

 pickerView.datePicker.minimumDate=[NSDate date];

To Disable Future date :

pickerView.datePicker.maximumDate=[NSDate date];

if you need this compulsory in your app you need to try on some 3rdparty app like this

Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
2

To hide future dates, use this code:

let now = Date();
pickerviewOutletname.maximumDate = now

To hide past dates, use this code:

let now = Date();
pickerviewOutletname.minimumDate = now
unwichtich
  • 13,712
  • 4
  • 53
  • 66
shahana mh
  • 67
  • 5
  • This is a terrible answer, how does the code answer the question?. Please read the SO guidelines before posting. – sparkitny Jan 09 '18 at 10:40