如何修复 UIDatePicker 中的最大年份而不是日期和月份?
How to fix the maximum year in UIDatePicker but not date and month?
我可以在UIPicker中通过[NSDate date]设置最大日期。但是有没有办法只设置最大年份。还有一件事我想要滚动 日期和月份 但 年份应该最多到 2015 年 .
在属性检查器中,您可以选择设置最大值。
有属性个UIDatePicker
@property(nonatomic, retain) NSDate *maximumDate
更多属性请访问link
I Think This Will Help You
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *currentDate = [NSDate date];
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setYear:30];
NSDate *maxDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
[comps setYear:-30];
NSDate *minDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
[datePicker setMaximumDate:maxDate];
[datePicker setMinimumDate:minDate];
//Get Current Date
NSDate *currentTime = [NSDate date];
[DatePicker setMinimumDate:currentTime];
尝试设置最大日期,它会将最大值完全设置为年份
NSString *dateString = @"01-02-2010";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// this is imporant - we set our input date format to match our input string
// if format doesn't match you'll get nil from your string, so be careful
[dateFormatter setDateFormat:@"dd-MM-yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateFromString];
[datePicker setMaximumDate: dateFromString];
我可以在UIPicker中通过[NSDate date]设置最大日期。但是有没有办法只设置最大年份。还有一件事我想要滚动 日期和月份 但 年份应该最多到 2015 年 .
在属性检查器中,您可以选择设置最大值。
有属性个UIDatePicker
@property(nonatomic, retain) NSDate *maximumDate
更多属性请访问link
I Think This Will Help You
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *currentDate = [NSDate date];
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setYear:30];
NSDate *maxDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
[comps setYear:-30];
NSDate *minDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
[datePicker setMaximumDate:maxDate];
[datePicker setMinimumDate:minDate];
//Get Current Date
NSDate *currentTime = [NSDate date];
[DatePicker setMinimumDate:currentTime];
尝试设置最大日期,它会将最大值完全设置为年份
NSString *dateString = @"01-02-2010";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// this is imporant - we set our input date format to match our input string
// if format doesn't match you'll get nil from your string, so be careful
[dateFormatter setDateFormat:@"dd-MM-yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateFromString];
[datePicker setMaximumDate: dateFromString];