我试图显示一个简单的日期选择器,其最大日期是今天,而最小值是100年前,但它根本不起作用且日期选择器没有限制。
CGRect frame = CGRectMake(0, CGRectGetHeight([UIScreen mainScreen].bounds)-250, CGRectGetWidth([UIScreen mainScreen].bounds), 250);
UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:frame];
datePicker.backgroundColor = [UIColor whiteColor];
datePicker.datePickerMode = UIDatePickerModeDate;
datePicker.minimumDate = [AppConfiguration currentDateMinusXYears:100];
datePicker.maximumDate = [NSDate date];
[datePicker addTarget:self action:@selector(datePicker:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:datePicker];
+ (NSDate *)currentDateMinusXYears:(NSInteger)xYears
{
NSDate *today = [NSDate new];
NSDateComponents *addComponents = [[NSDateComponents alloc] init];
addComponents.year = - xYears;
return [[NSCalendar currentCalendar] dateByAddingComponents:addComponents toDate:today options:0];
}
任何想法为什么?
编辑1: 我最终使用了第三方代码。
答案 0 :(得分:0)
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setYear:30];
NSDate *maxDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
[comps setYear:0];
NSDate *minDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
[actionSheetPicker setMaximumDate:maxDate];
[actionSheetPicker setMinimumDate:minDate];