ios - UIDatePicker:选择日期时,跳转到任意一个随机日期

标签 ios objective-c uidatepicker

长期以来我一直在努力解决这个问题,但没有找到解决这个问题的方法。

当我尝试选择年份时,有时日期选择器的年份部分会跳转到随机年份。 (同样的事情发生在日期和月份的选择上)

数据选择器包含在 UIView 中,我为其创建了一个 IBOutlet

- (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];

}

问题见下图...

enter image description here

我认为这可能是由于某些第 3 方库造成的。我的项目中有以下 pod

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

我该如何解决这个问题?

有什么帮助/建议吗?

更新

我要限制用户只能选择从 17 到 50 年的出生年份,这样 minDate 将从当前年份回溯 17 年 maxDate 将从当前年份倒退 50 年

如果不设置minDatemaxDate也会出现这个问题

也测试了以下情况

  • 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.

以下函数。当我更改日期选择器中的日期时调用。

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

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

}

最佳答案

奇怪的是,当我只是将(拖放)UIDatePicker View 放在屏幕上时,只是为了测试它的滚动问题。当我滚动日期时间选择器 View 时,它也在随机位置滚动。

我感觉是第三方库的问题。

经过几个小时的调试和试运行,我终于找到了导致这个问题的第三方库。

实际上是类别名称是 UITableView+SPXRevealAdditions

当我删除它时 UIDatePicker 工作正常,当我重新添加时问题开始出现。

这真的很奇怪,但对我有用。:D

更新:

我也想要那个第 3 方。所以我所做的是在 viewDidLoadMethod: 中添加了一个虚拟的平移手势。

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

和虚拟方法。

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

这解决了我的问题,现在 UIDatePicker 工作正常,第三方库也能正常工作...欢呼...:D。

关于ios - UIDatePicker:选择日期时,跳转到任意一个随机日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57952547/

相关文章:

ios - 如果语句不在 Swift 中呈现?

c# - 如何将 MonoTouch.Dialog View 封装到 View Controller 中?

objective-c - 在 iOS 中运行后台服务

ios - 在 ios 8.3 及更高版本中,UIAlertView 导致 keyboardWillShow 和 keyboardWillHide 被调用两次

ios - 滚动 tableView 时最小化导航栏

iPhone 日期选择器代替键盘?

ios - 带有 DatePicker 的 UITextField 崩溃

ios - 调整 UITableView 以允许使用键盘

ios - 当有人滚动超出 UICollectionView 的顶部时,我如何调用方法?

javascript - 有没有办法让 jQuery UI Datepicker 不可点击?