Search code examples
iosobjective-cuidatepicker

UIDatePicker: While selecting date, it jumps to any random date


I have been trying to fix this issue since so long but did not find any way to sort this out.

When I'm trying to select a year sometimes the year component of the date picker jumps to a random year. (The same thing happens with date and month selection)

The data picker is contained in a UIView and I have created an IBOutlet for it.

- (void)setupforBirthdates {

[self.dtPicker setDatePickerMode: UIDatePickerModeDate];


NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier: NSCalendarIdentifierGregorian];
NSDate *currentDate = [NSDate date];
NSDateComponents * comps = [[NSDateComponents alloc] init];
[comps setYear: -17];
NSDate * maxDate = [gregorian dateByAddingComponents: comps toDate: currentDate options: NSCalendarMatchStrictly];
[comps setYear: -50];
NSDate * minDate = [gregorian dateByAddingComponents: comps toDate: currentDate options: NSCalendarMatchStrictly];

[self.dtPicker setMinimumDate:minDate];
[self.dtPicker setMaximumDate:maxDate];
[self.dtPicker setDate:maxDate];

}

ISSUE See the below image...

enter image description here

I thought that it may be due to some 3rd party libraries this is happening. I have the following pod in my project

pod 'AFNetworking'
pod 'GoogleMaps'
pod 'GooglePlaces' 
pod 'Fabric'
pod 'Crashlytics'
pod 'RACollectionViewReorderableTripletLayout'
pod 'FBSDKLoginKit'
pod 'Socket.IO-Client-Swift', '~> 13.3.0'

How can I solve this issue?

Any help/suggestions?

UPDATE

I what to restrict user to select birth year from 17 to 50 year only so the minDate will be 17 years back from current year maxDate will be 50 years back from the current year

The issue is also coming if I don't set minDate and maxDate

Tested the following case too

  • I just drag and drop new date picker on screen. I did not set any min/max date, nor connect an IBOutlet and I run app and when I scroll to any date the same issue is coming

  • You will be surprised that I have created a custom date picker using UIPickerView and the issue is still there.

  • When I have created a new demo project and copy pasted the code in demo project then this issue is not coming. I've tested it many times on demo project the issue is not coming.

The following func. is called when I change the date in date picker.

- (IBAction)datePickerValueChanged:(id)sender {
  NSDateFormatter *dateFormat=[[NSDateFormatter alloc]init];
  [dateFormat setDateFormat:@"yyyy-MM-dd"];

  self.txtBirthday.text = [NSString stringWithFormat:@"%@",[dateFormat  stringFromDate:self.dtPicker.date]];

}

Solution

  • The strange thing is that for the When I just put(drag & drop) UIDatePicker View on a screen just to test its scrolling issue. When I scroll date time picker view then also it was scrolling at random location.

    I feel that it is the issue with third party library.

    After many hours of debugging and trail-run, I finally found the third party library which was the source of this issue.

    Actually is was the category name is UITableView+SPXRevealAdditions.

    When I removed it UIDatePicker is working fine and when I re-added the issue is started occurring.

    It is really very strange but It worked for me.:D

    Update:

    I wanted the that 3rd party too. So what I did id added a dummy pan gesture in viewDidLoadMethod:.

    UIPanGestureRecognizer *p = [[UIPanGestureRecognizer alloc] initWithTarget:nil action:@selector(doNothing)];
    [self.dtPicker addGestureRecognizer:p];
    

    and the dummy method.

    - (void)doNothing {
        printf("do nothing");
    }
    

    This solved my problem and Now the UIDatePicker is working fine and also third party library too...cheers...:D.