iOS8/9 | iOS10 |
---|---|
![]() |
![]() |
![]() |
---|
![]() |
---|
要自定义单元格,请在小节“Example-Swift”或“Example-Objc”中查看 DIY 示例
单选 滑动选择 |
多选 滑动选择 |
DIY 滑动选择 |
---|---|---|
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
---|
更多成就 在 FSCalendar 画廊 中可用
- 适用于 iOS8+ 👍
use_frameworks!
target '<Your Target Name>' do
pod 'FSCalendar'
end
- 适用于 iOS7+
target '<Your Target Name>' do
pod 'FSCalendar'
end
需要 NSCalendarExtension 以获得 iOS7 兼容性。
- 适用于 iOS8+
github "WenchaoD/FSCalendar"
添加依赖项
.package(url: "https://github.com/WenchaoD/FSCalendar.git", from: "2.8.4")
- 将
FSCalendar
文件夹下的所有文件拖入您的项目。 👍
或者为了测试运行,只需在
Example-Objc
或Example-Swift
中按command+u
,然后启动 UITest Target。
只有标记为“👍”的方法支持 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 中使用
FSCalendar
,您首先需要 创建 Bridge Header。
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
有关在 Swift3 中使用 FSCalendar 的详细信息,请参阅
Example-Swift
。
FSCalendar
不会 自动向帧更新,请实现
- 对于 AutoLayout
- (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()
}
在
Swift3
中,NSDate
和NSDateFormatter
已分别重命名为 Date 和 DateFormatter,详细信息请见Example-Swift
。
- 通过 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"];
- 使用 NSDateFormatter
self.formatter = [[NSDateFormatter alloc] init];
self.formatter.dateFormat = @"yyyy/MM/dd";
NSString *string = [self.formatter stringFromDate:date];
NSLog(@"Date is %@", string);
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 此存储库
支持与
支持与
如果你在你的应用中用这个库做出了漂亮的日历,请截屏并在twitter上 @我。你的帮助对我来说意义重大!
FSCalendar 在 MIT 许可下可用。有关更多信息,请参阅 LICENSE 文件。