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

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

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

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

数据选择器包含在 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];

}

问题见下图...

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

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

我该如何解决这个问题?

任何 help/suggestions?

更新

我如何限制用户 select 出生年份从 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]];

}

您可以通过将标签值与最小和最大日期进行比较来检查何时您的选择器想要更改标签值,这里是代码 *如果我的代码做错了,请交换升序和降序

- (IBAction)datePickerValueChanged:(id)sender {   
    NSDateFormatter *dateFormat=[[NSDateFormatter alloc]init];   [dateFormat setDateFormat:@"yyyy-MM-dd"];
    NSDate * maxDate = [gregorian dateByAddingComponents: comps toDate: currentDate options: NSCalendarMatchStrictly];
[comps setYear: -50];
    NSDate * minDate = [gregorian dateByAddingComponents: comps toDate: currentDate options: NSCalendarMatchStrictly];
    NSDate * curDate = self.dtPicker.date;
    if(([curDate compare:minDate] == NSOrderedAscending)&& ([curDate compare:maxDate] == NSOrderedDescending)){
      self.txtBirthday.text = [NSString stringWithFormat:@"%@",[dateFormat stringFromDate:self.dtPicker.date]];
    }

}
class ViewController: UIViewController {

    @IBOutlet weak var datePicker: UIDatePicker!
    @IBOutlet weak var label: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        self.configureDatePicker()


    }


    private func configureDatePicker() {

        let calendar = Calendar(identifier: .gregorian)
        // 17 years less
        let minimumDate = calendar.date(byAdding: .year, value: -17, to: Date())
        // 50 years max. 17 + 33 = 50
        let maximumDate = calendar.date(byAdding: .year, value: -50, to: Date())
        self.datePicker.minimumDate = minimumDate
        self.datePicker.maximumDate = maximumDate
    }


    @IBAction func onDatePickerChanged(sender: UIDatePicker) {

        let dateFormtter = DateFormatter()
        dateFormtter.dateStyle = .medium
        self.label.text = dateFormtter.string(from: sender.date)
    }
}

这段代码工作正常,没有任何问题。我认为您的代码中存在一些问题。 UIDatePicker 在某些地方正在重置。

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

感觉是第三方库的问题

经过数小时的调试和追踪-运行,我终于找到了导致此问题的第三方库。

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

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

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

更新:

我也想要那个第三者。所以我所做的是在 viewDidLoadMethod:.

中添加了一个虚拟平移手势
UIPanGestureRecognizer *p = [[UIPanGestureRecognizer alloc] initWithTarget:nil action:@selector(doNothing)];
[self.dtPicker addGestureRecognizer:p];

和虚拟方法。

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

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