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];
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.
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.
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];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With