在视图中选择日期字段时,将显示日期选择器。我想限制滚动,使其比当前月份少2个月,比当前月份多4个月。我见过类似的例子。例如:对于当前的11月,月份范围应为明年的9月-3月。
我测试了此示例,但未成功。
// 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];
}
更新:上面的代码更新日期选择器对象。但是,问题在于日期选择器视图中没有更改。对于上面的示例,我希望滚动条限制从明年9月到明年3月的滚动。这样,用户在滚动时不会感到不知所措。从更好的UX角度来看。
好的,基于广泛的讨论,我现在了解到,目标是删除超出范围的月份。
您无法使用标准的日期选择器执行此操作。您将必须使用来构建自定义控件UIPickerView
。
简短的回答是:“不要那样做。”
如果您希望允许2个月后和4个月前:
如果您当前的日期是2016年11月:
有效月份为9月,2016年11月,10月,2016年12月以及2017年1月,2月和2017年3月。
因此,如果用户选择2016,则需要在9月,10月和11月填充月份选择器轮。然后,如果用户切换到2017年,则只需要重建一月,二月和三月的月轮。
但是,现在你没有使用日期选择器,您使用的是UIPickerView
要采取行动像一个日期选择器。
因此,除了根据当前年份重建月份微调器之外,您还必须管理设置日期选择器以显示当前所选年份和月份的有效日期。大多数月份有30或31天,但是2月呢?leap年呢?几百年了,那不是leap年?
另外,您正在构建一个看起来像日期选择器的控件,而某种(但不完全)像日期选择器的行为。
除了公历之外,您还要处理日历吗?希伯来语,阿拉伯语,中文日历怎么办?要使所有这些正确工作是您的负担。
苹果已经在可用性研究上花费了很多钱,并提出了一个效果很好的标准控件,并且确实处理了不同日历系统等的本地化。
简而言之,这是您不希望陷入的兔子洞。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句