Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date picker scrolls to past date, even though minimum date is set to current date

Strange behavior in iOS 6.1 I have set the minimum date to current date for my date picker like this

NSDate *currentTime = [NSDate date];
[picker setMinimumDate:currentTime];

enter image description here

But when I run the app I am able to scroll to past date, though its not selected, picker doesn't jump back to current date. It's happening only with iOS 6.1 version and in rest picker is behaving normally.

like image 798
Ankit Jain Avatar asked Mar 20 '13 06:03

Ankit Jain


2 Answers

I got the same issue as you and fixed it with only setting the date to the maximum date manually (in this case I set the limit to the current date):

- (IBAction)pickerValueChanged:(id)sender {

    dispatch_async(dispatch_get_main_queue(), ^{
        UIDatePicker *datePicker = (UIDatePicker *)sender;

        if ([self.datePicker.date compare:[NSDate date]] == NSOrderedDescending) {

            datePicker.date = [NSDate date];
        }

    });
}

This function is triggered when the date value from the date picker did change. you can set a maximum or minimum value here.

like image 159
Adrien G Avatar answered Oct 02 '22 07:10

Adrien G


Try This code

 NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
 NSDate *currentDate = [NSDate date];
 NSDateComponents *comps = [[[NSDateComponents alloc] init] autorelease];
 [comps setYear:30];
 NSDate *maxDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
 [comps setYear:-30];
 NSDate *minDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];

 [datePicker setMaximumDate:maxDate];
 [datePicker setMinimumDate:minDate];
like image 38
moosa0709 Avatar answered Oct 02 '22 06:10

moosa0709