https://github.com/WenchaoD/FSCalendar

使用cocoapods集成

pod 'FSCalendar'

注意事项

FSCalendar doesn’t update frame by itself, Please implement

也就是说更新(修改)约束(frame)需要实现它的代理方法:

- (void)calendar:(FSCalendar *)calendar boundingRectWillChange:(CGRect)bounds animated:(BOOL)animated
{
    //AutoLayout    
    self.calendarHeightConstraint.constant = CGRectGetHeight(bounds);
    [self.view layoutIfNeeded];
    //Manual Layout
    //calendar.frame = (CGRect){calendar.frame.origin,bounds.size};
    
    //Masonry
    //[calendar mas_updateConstraints:^(MASConstraintMaker *make) {
    //    make.height.equalTo(@(bounds.size.height));
    //}];
    //[self.view layoutIfNeeded];
}

使用

初始化

支持xib和纯代码:

- (void)viewDidLoad {
    [super viewDidLoad];
    
    FSCalendar *calendar = [[FSCalendar alloc] initWithFrame:CGRectZero];
    calendar.dataSource = self;
    calendar.delegate = self;
    [self.view addSubview:calendar];
    self.calendar = calendar;
        
    calendar.translatesAutoresizingMaskIntoConstraints = NO;    
    NSArray *constraints1 = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[calendar]-|" options:0 metrics:nil views:@{@"calendar":calendar}];
    NSArray *constraints2 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-20-[calendar(==400)]" options:0 metrics:nil views:@{@"calendar":calendar}];
    [self.view addConstraints:constraints1];
    [self.view addConstraints:constraints2];
}

基本配置

//星期一为一周的第一天
_calendar.firstWeekday = 2;
//允许多选
_calendar.allowsMultipleSelection = NO;
//意思是不在本月显示其他月的信息
_calendar.placeholderType = FSCalendarPlaceholderTypeNone;

//配色
_calendar.appearance.weekdayTextColor = [UIColor grayColor];//FSCalendarStandardTitleTextColor;
_calendar.appearance.headerTitleColor = FSCalendarStandardTitleTextColor;
//_calendar.appearance.eventDefaultColor = [UIColor redColor]; //默认小点的颜色
_calendar.appearance.selectionColor = [UIColor blackColor]; //选中点点的灰色
_calendar.appearance.headerDateFormat = @"yyyy年M月";
_calendar.appearance.todayColor = FSCalendarStandardTodayColor;
_calendar.appearance.borderRadius = 1.0;
_calendar.appearance.headerMinimumDissolvedAlpha = 0.2;

跳转到当天所在的视图:

[self.calendar setCurrentPage:[NSDate date] animated:YES];

跳转上一月、下一月:

- (void)previousClicked:(id)sender
{
    NSDate *currentMonth = self.calendar.currentPage;
    NSDate *previousMonth = [self.gregorian dateByAddingUnit:NSCalendarUnitMonth value:-1 toDate:currentMonth options:0];
    [self.calendar setCurrentPage:previousMonth animated:YES];
}

- (void)nextClicked:(id)sender
{
    NSDate *currentMonth = self.calendar.currentPage;
    NSDate *nextMonth = [self.gregorian dateByAddingUnit:NSCalendarUnitMonth value:1 toDate:currentMonth options:0];
    [self.calendar setCurrentPage:nextMonth animated:YES];
}

代理方法

FSCalendarDelegate

控制选中/取消选中、页面跳转下一页回调等操作

FSCalendarDataSource

日历数据源,控制展示什么。 如日历的上下限范围(minimumDateForCalendar:maximumDateForCalendar:)、标题和副标题的显示、事件的控制(日期下面的小点)等

FSCalendarDelegateAppearance

控制控件的位置、颜色等。

其他

随便写的demo