IOS之使用CKCalendar做datePicker
比较符合项目需要,要做一个日历选择器,并且实现当前日期之后的时间不可选。网上找了一下,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]; } }
完!
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。