iOS UIDatePicker设置允许最小日期、最大日期范围

来源:互联网 发布:雅思作文知乎simon 编辑:程序博客网 时间:2024/04/30 05:36
设置UIDatePicker的允许最大时间、最小时间:
self.frame = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height);datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 44, CGRectGetWidth(self.frame), 216)];datePicker.datePickerMode = UIDatePickerModeDate;datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"zh"];datePicker.backgroundColor = [UIColor whiteColor];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];NSDate *currentDate = [NSDate date];NSDateComponents *comps = [[NSDateComponents alloc] init];[comps setYear:10];//设置最大时间为:当前时间推后十年NSDate *maxDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];[comps setYear:-10];//设置最小时间为:当前时间前推十年NSDate *minDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
[datePicker setMaximumDate:maxDate];[datePicker setMinimumDate:minDate];

1 0
原创粉丝点击