[英]UIDatePicker min and max doesn't get changed
我試圖顯示一個簡單的日期選擇器,最大日期是今天,最小日期是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:我最終使用了第三方代碼。
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];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.