#import <UIKit/UIKit.h>
一、初始化
if (@available(iOS 16.0, *)) {
UICalendarView *calen = [[UICalendarView alloc]initWithFrame:UIScreen.mainScreen.bounds];
calen.backgroundColor = WHColor;
calen.tintColor = [UIColor orangeColor];
calen.calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
calen.fontDesign = UIFontDescriptorSystemDesignRounded;
calen.delegate = self;
UICalendarSelectionMultiDate *multiDateSelection = [[UICalendarSelectionMultiDate alloc]initWithDelegate:self];
calen.selectionBehavior = multiDateSelection;
[self.view addSubview:calen];
} else {
// Fallback on earlier versions
}
二、代理<UICalendarViewDelegate,UICalendarSelectionMultiDateDelegate>
-(void)calendarView:(UICalendarView *)calendarView didChangeVisibleDateComponentsFrom:(NSDateComponents *)previousDateComponents API_AVAILABLE(ios(16.0)){
NSString *year = [NSString stringWithFormat:@"%ld", (long)previousDateComponents.year];
NSString *month = [NSString stringWithFormat:@"%ld", (long)previousDateComponents.month];
NSString *day = [NSString stringWithFormat:@"%ld", (long)previousDateComponents.day];
NSString *hour = [NSString stringWithFormat:@"%ld", (long)previousDateComponents.hour];
DLog(@"%@-%@-%@-%@",year,month,day,hour);
}
-(void)multiDateSelection:(UICalendarSelectionMultiDate *)selection didSelectDate:(NSDateComponents *)dateComponents API_AVAILABLE(ios(16.0)){
NSString *year = [NSString stringWithFormat:@"%ld", (long)dateComponents.year];
NSString *month = [NSString stringWithFormat:@"%ld", (long)dateComponents.month];
NSString *day = [NSString stringWithFormat:@"%ld", (long)dateComponents.day];
NSString *hour = [NSString stringWithFormat:@"%ld", (long)dateComponents.hour];
DLog(@"%@-%@-%@-%@",year,month,day,hour);
}
-(void)multiDateSelection:(UICalendarSelectionMultiDate *)selection didDeselectDate:(NSDateComponents *)dateComponents API_AVAILABLE(ios(16.0)){
NSString *year = [NSString stringWithFormat:@"%ld", (long)dateComponents.year];
NSString *month = [NSString stringWithFormat:@"%ld", (long)dateComponents.month];
NSString *day = [NSString stringWithFormat:@"%ld", (long)dateComponents.day];
NSString *hour = [NSString stringWithFormat:@"%ld", (long)dateComponents.hour];
DLog(@"YY%@-%@-%@-%@",year,month,day,hour);
}
-(BOOL)multiDateSelection:(UICalendarSelectionMultiDate *)selection canSelectDate:(NSDateComponents *)dateComponents API_AVAILABLE(ios(16.0)){
NSString *year = [NSString stringWithFormat:@"%ld", (long)dateComponents.year];
NSString *month = [NSString stringWithFormat:@"%ld", (long)dateComponents.month];
NSString *day = [NSString stringWithFormat:@"%ld", (long)dateComponents.day];
NSString *hour = [NSString stringWithFormat:@"%ld", (long)dateComponents.hour];
DLog(@"TTS%@-%@-%@-%@",year,month,day,hour);
return YES;
}
-(BOOL)multiDateSelection:(UICalendarSelectionMultiDate *)selection canDeselectDate:(NSDateComponents *)dateComponents API_AVAILABLE(ios(16.0)){
NSString *year = [NSString stringWithFormat:@"%ld", (long)dateComponents.year];
NSString *month = [NSString stringWithFormat:@"%ld", (long)dateComponents.month];
NSString *day = [NSString stringWithFormat:@"%ld", (long)dateComponents.day];
NSString *hour = [NSString stringWithFormat:@"%ld", (long)dateComponents.hour];
DLog(@"TT%@-%@-%@-%@",year,month,day,hour);
return YES;
}
三、控件图片