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

4

5

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.

Obsecrate answered 20/3, 2013 at 6:59 Comment(0)
C
8

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.

Cerous answered 6/5, 2013 at 18:39 Comment(4)
Is this a question or an answer? If you have a question, please ask a new question stackoverflow.com/questions/askConias
Answer with a bonus question ;)Cerous
Please remove your "bonus" question (you won't get any answer that way, and it clutters the actual answer) - if you really want your question answered, please ask a new question.Conias
Actually datepicker returns previous date instead of frezzed date.so compare is not possibleWitten
G
2

You have to set min and max date like:

NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDate *currentDate = [NSDate date];
NSDateComponents *comps = [[[NSDateComponents alloc] init] autorelease];

NSDate *minDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
[datePicker setMinimumDate:minDate];
Gibeonite answered 20/3, 2013 at 7:9 Comment(0)
C
2

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];
Carpenter answered 20/3, 2013 at 7:11 Comment(6)
Realy you get the currunt date like your answer Way..?Gibeonite
@Prem i have tried your solution and it doesn't work. Is that the bug in iOS 6.1 ?Obsecrate
code4app.net/ios/Local-Notifier/4fb5e7c06803fa3a7f000000 I got the sample from this link..Teach me if any wrong..thksCarpenter
@NitinGohel Yes i am facing problem only in iOS 6.1 and in rest all its fine.Obsecrate
@Prem I have downloaded the source code for notifier like you sent. In that app i am able to scroll to past dates. My requirement is when user scrolls to past date picker must bring him back to current date. As there is no delegate method for date picker to handle this situation how do i control it ? Main thing is, this strange behavior is in iOS 6.1 only.Obsecrate
@Prem Thanks for helping, but problem stil remains the same. I am able to scroll to past date. You can check this feature in other iOS versions it works perfect but in iOS 6.1 its causing trouble. If its bug in iOS 6.1 any document says this let me know.Obsecrate
T
1

The property is an NSDate object or nil (the default), which means no maximum date. This property, along with the minimumDate property, lets you specify a valid date range. If the minimum date value is greater than the maximum date value, both properties are ignored. The minimum and maximum dates are also ignored in the countdown-timer mode (UIDatePickerModeCountDownTimer).

Try setting valid minimum and maximum date both. It works for me.

Tenpin answered 18/3, 2015 at 12:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.