Search code examples
iosobjective-cuidatepicker

How to disable future dates,months and year in UIDatePicker?


How to hide the future dates in UIDatePicker for user choosing only past and current year of birthdays.

I search lot of source but I can't get the desired result.

Here is my code,

 dateofBirthDatePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0.0, 44.0, 0.0, 0.0)];

    dateofBirthDatePicker.datePickerMode = UIDatePickerModeDate;
    [dateofBirthDatePicker setBackgroundColor:DATE_PICKER_GRAY_COLOR];

//    UILabel *label = [UILabel appearanceWhenContainedIn:[UITableView class],[UIDatePicker class], nil];
//    label.font = HELVETICA_NEUE(24);
//    label.textColor = GREEN_COLOR;
    [dateofBirthDatePicker addTarget:self
                              action:@selector(LabelChange:)
                    forControlEvents:UIControlEventValueChanged];
    dateofbirthTextField.inputView = dateofBirthDatePicker;
    [self datePickerToolBar];
}

- (void)LabelChange:(id)sender
{
    NSDateFormatter *dateFormat= [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"dd/MM/yyyy"];
    dateofbirthTextField.text = [NSString stringWithFormat:@"%@",
                                [dateFormat stringFromDate:dateofBirthDatePicker.date]];
}

If any body know the solution kindly give the suggestion. I really appreciate to you.


Solution

  • In Swift 2:

    self.datePicker.maximumDate = NSDate()

    In Swift 3:

    self.datePicker.maximumDate = Date()