1

When you select the date field in a view, the date picker displays. I want to limit the scroll from a minimum 2 months less than current month and a maximum 4 months more than current month. I have seen similar examples. For example: for current month November, the month range should be September - March next year.

I tested this example, but wasn't successful.

// Configure date picker
- (void) initializeDatePicker {
    _datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 0, 600, 160)];
    _datePicker.backgroundColor = [UIColor whiteColor];
    _datePicker.datePickerMode = UIDatePickerModeDate;
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDate *currentDate = [NSDate date];
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setMonth:-2];
NSDate *minDate = [gregorian dateByAddingComponents:comps toDate:currentDate  options:0];
[comps setMonth:4];
NSDate *maxDate = [gregorian dateByAddingComponents:comps toDate:currentDate  options:0];

_datePicker.minimumDate = minDate;
_datePicker.maximumDate = maxDate;


[_datePicker addTarget:self action:@selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
[_txfDateTextField setInputView:_datePicker];
selectedDate =[NSDate date];
}

UPDATE: The above code updates the date picker object. However, the problem is that there is no change in the date picker view. For the example above, I want the scroll to limit scrolling from September to March next year. This way the user doesn't get overwhelmed when he is scrolling. From a better UX point of view.

Community
  • 1
  • 1
Ron
  • 389
  • 1
  • 4
  • 16
  • Can you post some of your own code that you tried? – AdamPro13 Nov 17 '16 at 21:25
  • @AdamPro13 I have updated the post... – Ron Nov 17 '16 at 21:26
  • Your question isn't very clear. You have an input field that displays a date. When the user selects it you display a date picture and let the user enter a date. You've talked about a single date field. What do you mean when you refer to a "current month"? If the user enters a new date, isn't that a new current date? Or are you actually having the user select 2 different dates? – Duncan C Nov 17 '16 at 21:31
  • For starters, I think you want to use `-2` instead of `2`. – AdamPro13 Nov 17 '16 at 21:36
  • @AdamPro13 Yeah... I used -2 instead. Sorry. – Ron Nov 17 '16 at 21:37
  • What results are you actually seeing? – AdamPro13 Nov 17 '16 at 21:38
  • @DuncanC The date picker shows date in the format: November 17 2017. I am referring to the month part. When a user scrolls, I want him to be able to scroll to a minimum September (here) and a maximum March(here). There is only one date picker. I hope this makes it clear. – Ron Nov 17 '16 at 21:40
  • Yes, there is only one date picker because a `UIDatePicker` is intended to allow a user to select a single date, not a range of dates. The `minimumDate` and `maximumDate` properties determine the range of dates you can select in the single date picker. – AdamPro13 Nov 17 '16 at 21:44
  • @AdamPro13. Yes. That was meant to explain Duncan For your previous comment, the date picker object is updated with the correct results, but not the datepickerView which is my problem. so, the minimum date in the object is 2016-09-17 and the maximum is 2017-03-17. I want this in the month scroll. I will update my question.Thanks for your support. – Ron Nov 17 '16 at 21:53
  • You say "...I want the minimum and maximum range to be dynamic, meaning it should update itself, as I scroll." What does that mean? If you set a minimum and maximum that limits the range of dates the user can select, but if the user scrolls the range changes then how is there a limit? "We start with November 17" You can't pick a date >2 months less than that or >4 months after that so the limits are September through march 2017. now the user scrolls to September 17, and you want the earliest date limit to now change to 2 months earlier than that, July 17? – Duncan C Nov 17 '16 at 21:59
  • 1
    If the date limits change/expand as you scroll, how are they limits at all? It doesn't make sense. – Duncan C Nov 17 '16 at 21:59
  • @DuncanC. I realize what you mean.. Lets say I don't what to update then and just set the range. I apologize for the mistake. It's just because the datepicker behaves differently at different places of the app. – Ron Nov 17 '16 at 22:03
  • So the bottom line is that your text field doesn't change when the user selects a new date? Or that the ranges on the date picker are not being inforced? Or some other problem? I still don't understand what you're asking, and am about to give up. – Duncan C Nov 17 '16 at 22:17
  • @DuncanC..The very last time...My problem is only that my ranges on the date picker are not being inforced. Perfectly that. Nothing else. – Ron Nov 17 '16 at 22:23
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/128466/discussion-between-duncan-c-and-ron). – Duncan C Nov 18 '16 at 15:27

1 Answers1

1

Ok, based on an extended discussion, I now understand that the goal is to remove months that are outside of the range.

You can't do it with a standard date picker. You would have to build a custom control using a UIPickerView.

The short answer is, "Don't do that."

If you want to allow 2 months back, and 4 months forward:

If your current date is November, 2016:

Valid months are September, October, November 2016, December 2016, and January, February, and March 2017.

So if the user selects 2016, you'd need to populate the month picker wheel with September, October, November. Then if the user switches to 2017, you'd need to rebuild the month wheel with only January, February, and March.

But, now you're not using a date picker, you're using a UIPickerViewthat you want to act like a date picker.

So in addition to rebuilding the month spinner based on the current year, you have to manage setting up the day picker to show the valid days for the currently selected year and month. Most months have 30 or 31 days, but what about February? What about leap years? What about centuries, which aren't leap years?

Plus, you're building a control that looks like a date picker, and sort of, but not quite, acts like a date picker.

Are you going handle calendars other than Gregorian? What about Hebrew, Arabic, Chinese calendars? The burden is on you to make all of that work correctly.

Apple has spent a LOT of money on usability studies and come up with a standard control that works well, and does handle localization for different calendar systems, etc, etc.

In short, this is a rabbit hole you do not want to descend into.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • Yes. I totally agree with you. Despite of the work involved in doing this, it would still not give us the best user experience at all. Besides, as the picker would be highly customized, it would work differently from all other apps in app store, hence, might also confuse the user in a similar way. The cost of implementation by far, outweighs the benefits. Hence, I would prefer to leave it as is. Thanks for your detailed input. – Ron Nov 21 '16 at 18:19