比较符合项目需要,要做一个日历选择器,并且实现当前日期之后的时间不可选。网上找了一下,GitHub上的一个开源日历选择器CKCalendar比较符合项目需求。效果图如下:(很反感网上那些文章讲了半天连个效果图都没有,纯个人吐槽,勿喷~)
刚开始对着CKCalendar的源码看来半天,还想着直接修改源码让当前日期之后的不可选,却忘了去测试作者提供的代理接口,真傻!
鼓捣半天后终于弄出来了,怕时间久了就忘了,还是做一下笔记:
当前日期之后不可选:
#pragma mark - CalendarPickerDelegate Method - (BOOL)calendar:(CKCalendarView *)calendar willSelectDate:(NSDate *)date { NSDate *now = [NSDate date]; NSString *stringNow = [DATE_FORMATER stringFromDate:now]; now = [DATE_FORMATER dateFromString:stringNow]; NSString *selectedValue; if ([date compare:now] == NSOrderedSame ||[date compare:now] == NSOrderedAscending) { _calendarPickerVC.selectedDate = date; selectedValue = [DATE_FORMATER stringFromDate:_calendarPickerVC.selectedDate]; _evalItem.value0 = selectedValue; [_btnValue0ValueList setTitle:selectedValue forState:UIControlStateNormal]; [self makeOtherItemValues]; } else return NO; return YES; } - (void)calendar:(CKCalendarView *)calendar configureDateItem:(CKDateItem *)dateItem forDate:(NSDate *)date { NSDate *now = [NSDate date]; NSString *stringNow = [DATE_FORMATER stringFromDate:now]; now = [DATE_FORMATER dateFromString:stringNow]; if ([date compare:now]==NSOrderedDescending ) { dateItem.textColor = [UIColor lightGrayColor]; } }
当前日期之前不可选:
#pragma mark - CalendarPickerDelegate Menthod - (BOOL)calendar:(CKCalendarView *)calendar willSelectDate:(NSDate *)date { NSDate *now = [NSDate date]; NSString *stringNow = [DATE_FORMATER stringFromDate:now]; now = [DATE_FORMATER dateFromString:stringNow]; NSString *selectedValue; if ([date compare:now] == NSOrderedSame ||[date compare:now] == NSOrderedDescending) { _calendarPickerVC.selectedDate = date; selectedValue = [DATE_FORMATER stringFromDate:_calendarPickerVC.selectedDate]; } else return NO; return YES; } - (void)calendar:(CKCalendarView *)calendar configureDateItem:(CKDateItem *)dateItem forDate:(NSDate *)date { NSDate *now = [NSDate date]; NSString *stringNow = [DATE_FORMATER stringFromDate:now]; now = [DATE_FORMATER dateFromString:stringNow]; if ([date compare:now]==NSOrderedAscending ) { dateItem.textColor = [UIColor lightGrayColor]; } }
特别记得不要忘记设置代理: _calendarPickerVC.calendar.delegate =
self;
完!
IOS之使用CKCalendar做datePicker
时间: 2024-11-05 19:40:51