How to disable infinite scrolling in UIDatePicker?

自古美人都是妖i 提交于 2019-12-13 07:42:58

问题


I am implementing something with a date selection and it turns out that Apple's implementation of UIDatePicker uses infinite loop for days and months. In plain english, it is impossible to hit a ceiling of 1 nor the bottom that is 12 or 31. It just loops forever starting both sets again with 1 and so on which is from my very subjective perspective the stupidest idea on this UI component.

The UX with this picker is such that the speed of spinning is very fast so it is actually difficult to hit the right number. If it is november, and I need february, it would be much faster to just hit the top with fast vert. swipe, and then do a slight move down to Feb, which I would prefer over a few shaky moves until I find Feb in the infinite set.

Looking at the class reference, I can't seem to find anything. Nor it's superclass seems to have a suitable property.

Any appstore-safe ideas (exc. custom implementation..as a last resort)?


回答1:


I have used this code below from another stackoverflow question in the past and works well.

How to set Minimum time to appear as default in UIDatepicker everytime

int startHour = 7;
int endHour = 11;

NSDate *date1 = [NSDate date];
NSCalendar *gregorian = [[NSCalendar      alloc] initWithCalendarIdentifier: NSGregorianCalendar];
 NSDateComponents *components = [gregorian components: NSUIntegerMax fromDate: date1];
[components setHour: startHour];
[components setMinute: 0];
[components setSecond: 0];
NSDate *startDate = [gregorian dateFromComponents: components];

[components setHour: endHour];
[components setMinute: 0];
[components setSecond: 0];
NSDate *endDate = [gregorian dateFromComponents: components];

[userTimePicker setDatePickerMode:UIDatePickerModeTime];
[userTimePicker setMinimumDate:startDate];
[userTimePicker setMaximumDate:endDate];
[userTimePicker setDate:startDate animated:YES];
[userTimePicker reloadInputViews];


来源:https://stackoverflow.com/questions/29198769/how-to-disable-infinite-scrolling-in-uidatepicker

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!