iOS8/9 | iOS10 |
---|---|
![]() |
![]() |
![]() |
---|
![]() |
---|
要自定义你的单元格,请查看
Example-Swift
或Example-Objc
中的 DIY 示例
单选 滑动选择 |
多选 滑动选择 |
DIY 滑动选择 |
---|---|---|
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
---|
use_frameworks!
target '<Your Target Name>' do
pod 'FSCalendar'
end
target '<Your Target Name>' do
pod 'FSCalendar'
end
需要 NSCalendarExtension 以获得 iOS7 的兼容性。
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 Scene 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;
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
*不会* 自行更新 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
}
- (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];
}
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
以了解详细信息。
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
self.formatter = [[NSDateFormatter alloc] init];
self.formatter.dateFormat = @"yyyy-MM-dd";
然后
NSDate *date = [self.formatter dateFromString:@"2016-09-10"];
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];
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
如果您使用此库在您的应用程序中制作了一个漂亮的日历,请截取屏幕截图并在 Twitter 上 @我。您的帮助对我来说意义重大!
FSCalendar 在 MIT 许可证下可用。 有关更多信息,请参见 LICENSE 文件。