目录
屏幕截图
iPhone
iPad
安全方向
今日扩展
iOS8/9 | iOS10 |
---|---|
![]() |
![]() |
交互式范围手势
![]() |
---|
DIY支持
![]() |
---|
要自定义您的单元格,请查看
Example-Swift
或Example-Objc
中的DIY示例
滑动选择
单选 滑动选择 |
多选 滑动选择 |
DIY 滑动选择 |
---|---|---|
![]() |
![]() |
![]() |
用户成就
![]() |
![]() |
![]() |
![]() |
---|
更多成就可在FSCalendar 示例库中找到。
安装
CocoaPods
- 适用于iOS8+
👍
use_frameworks!
target '<Your Target Name>' do
pod 'FSCalendar'
end
- 适用于iOS7+
target '<Your Target Name>' do
pod 'FSCalendar'
end
需要NSCalendarExtension以实现iOS7兼容性。
Carthage
- 适用于iOS8+
github "WenchaoD/FSCalendar"
SPM
添加依赖项
.package(url: "https://github.com/WenchaoD/FSCalendar.git", from: "2.8.2")
手动
- 将
文件夹下的所有文件拖入您的项目中。 👍
或者,在
的 Example-Objc
或Example-Swift
示例中按下command+u
并运行UI测试目标进行测试。
只有标记了"的函数支持IBInspectable / IBDesignable功能。👍 在Interface builder中享受乐趣
设置
使用界面构建器
1、 将 UIView 对象拖拽到 ViewController 场景中 2、 将 Custom Class
修改为 FSCalendar
3、 将 dataSource
和 delegate
链接到 ViewController
4、 最后,在您的 ViewController
中实现 FSCalendarDataSource
和 FSCalendarDelegate
或使用代码
@property (weak , nonatomic) FSCalendar *calendar;
// In loadView(Recommended) or viewDidLoad
FSCalendar *calendar = [[FSCalendar alloc] initWithFrame:CGRectMake(0, 0, 320, 300)];
calendar.dataSource = self;
calendar.delegate = self;
[self.view addSubview:calendar];
self.calendar = calendar;
或 Swift
- 使用 Swift 中的
FSCalendar
,您需要先 创建桥接头。
fileprivate weak var calendar: FSCalendar!
// In loadView or viewDidLoad
let calendar = FSCalendar(frame: CGRect(x: 0, y: 0, width: 320, height: 300))
calendar.dataSource = self
calendar.delegate = self
view.addSubview(calendar)
self.calendar = calendar
要使用 FSCalendar 在 Swift3 中,请参阅
Example-Swift
以获取详细信息。
警告
FSCalendar
不 会自动更新 frame,请实现
- 对于 自动布局
- (void)calendar:(FSCalendar *)calendar boundingRectWillChange:(CGRect)bounds animated:(BOOL)animated
{
self.calendarHeightConstraint.constant = CGRectGetHeight(bounds);
// Do other updates here
[self.view layoutIfNeeded];
}
- 对于 手动布局
- (void)calendar:(FSCalendar *)calendar boundingRectWillChange:(CGRect)bounds animated:(BOOL)animated
{
calendar.frame = (CGRect){calendar.frame.origin,bounds.size};
// Do other updates here
}
- 如果您使用 Masonry
- (void)calendar:(FSCalendar *)calendar boundingRectWillChange:(CGRect)bounds animated:(BOOL)animated
{
[calendar mas_updateConstraints:^(MASConstraintMaker *make) {
make.height.equalTo(@(bounds.size.height));
// Do other updates
}];
[self.view layoutIfNeeded];
}
- 如果您使用 SnapKit
func calendar(_ calendar: FSCalendar, boundingRectWillChange bounds: CGRect, animated: Bool) {
calendar.snp.updateConstraints { (make) in
make.height.equalTo(bounds.height)
// Do other updates
}
self.view.layoutIfNeeded()
}
使用 Interface Builder
预备知识
在
Swift3
中,NSDate
和NSDateFormatter
已分别重命名为 Date 和 DateFormatter,详情请参考Example-Swift
。
如何创建 NSDate 对象
- 通过 NSCalendar。
self.gregorian = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
然后
NSDate *date = [gregorian dateWithEra:1 year:2016 month:9 day:10 hour:0 minute:0 second:0 nanosecond:0];
// 2016-09-10 00:00:00
- 或者通过 NSDateFormatter
self.formatter = [[NSDateFormatter alloc] init];
self.formatter.dateFormat = @"yyyy-MM-dd";
然后
NSDate *date = [self.formatter dateFromString:@"2016-09-10"];
如何打印输出 NSDate 对象
- 使用 NSDateFormatter
self.formatter = [[NSDateFormatter alloc] init];
self.formatter.dateFormat = @"yyyy/MM/dd";
NSString *string = [self.formatter stringFromDate:date];
NSLog(@"Date is %@", string);
如何使用 NSCalendar 操作 NSDate
self.gregorian = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
- 获取 NSDate 的组件
NSInteger era = [self.gregorian component:NSCalendarUnitEra fromDate:date];
NSInteger year = [self.gregorian component:NSCalendarUnitYear fromDate:date];
NSInteger month = [self.gregorian component:NSCalendarUnitMonth fromDate:date];
NSInteger day = [self.gregorian component:NSCalendarUnitDay fromDate:date];
NSInteger hour = [self.gregorian component:NSCalendarUnitHour fromDate:date];
NSInteger minute = [self.gregorian component:NSCalendarUnitMinute fromDate:date];
...
- 获取下一个月
NSDate *nextMonth = [self.gregorain dateByAddingUnit:NSCalendarUnitMonth value:1 toDate:date options:0];
- 获取下一天
NSDate *nextDay = [self.gregorain dateByAddingUnit:NSCalendarUnitDay value:1 toDate:date options:0];
- 判断日期是否在今天/明天/昨天/周末
BOOL isToday = [self.gregorian isDateInToday:date];
BOOL isYesterday = [self.gregorian isDateInYesterday:date];
BOOL isTomorrow = [self.gregorian isDateInTomorrow:date];
BOOL isWeekend = [self.gregorian isDateInWeekend:date];
- 比较两个日期
BOOL sameDay = [self.gregorian isDate:date1 inSameDayAsDate:date2];
// Yes if the date1 and date2 are in same day
[self.gregorian compareDate:date1 toDate:date2 toUnitGranularity:unit];
// compare the era/year/month/day/hour/minute .etc ...
// return NSOrderAscending/NSOrderSame/NSOrderDecending
BOOL inSameUnit = [self.gregorian isDate:date1 equalToDate:date2 toUnitGranularity:unit];
// if the given unit (era/year/month/day/hour/minute .etc) are the same
支持此仓库
- ★Star 此仓库
* 支持使用
* 支持使用
联系
如果您在使用本库为您的应用程序创建了一个漂亮的日历,请截图并发送给@me,在我的Twitter上。您的帮助对我来说意义重大!
许可
FSCalendar 以MIT许可可用。有关更多信息,请参阅LICENSE文件。