FSCalendar Documentation

repository·master·Indexed 27 days ago

https://github.com/wenchaod/fscalendar

A highly customizable calendar component for iOS supporting Objective-C and Swift. Features include interactive gestures, DIY cell customization, support for Today Extensions, multiple date selection, and flexible scroll directions. The library can be installed via CocoaPods, Carthage, Swift Package Manager (SPM), or manual integration.

Tokens
2K
Snippets
11
Records
19
Agent score
45%

What's inside FSCalendar

  1. Setup FSCalendar using Interface Builder

    master
    1. Drag a UIView object onto your ViewController Scene.
    2. Change the Custom Class to FSCalendar.
    3. Link the dataSource and delegate outlets to your ViewController.
    4. Implement the FSCalendarDataSource and FSCalendarDelegate protocols in your ViewController.
  2. Handle dynamic frame updates for FSCalendar

    master

    FSCalendar does not update its frame automatically. You must implement the boundingRectWillChange:animated: delegate method to update your layout when the calendar's bounds change.

    // For AutoLayout
    - (void)calendar:(FSCalendar *)calendar boundingRectWillChange:(CGRect)bounds animated:(BOOL)animated
    {
        self.calendarHeightConstraint.constant = CGRectGetHeight(bounds);
        [self.view layoutIfNeeded];
    }
    
    // For Manual Layout
    - (void)calendar:(FSCalendar *)calendar boundingRectWillChange:(CGRect)bounds animated:(BOOL)animated
    {
        calendar.frame = (CGRect){calendar.frame.origin,bounds.size};
    }
    
    // For Masonry
    - (void)calendar:(FSCalendar *)calendar boundingRectWillChange:(CGRect)bounds animated:(BOOL)animated
    {
        [calendar mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@(bounds.size.height));
        }];
        [self.view layoutIfNeeded];
    }
    
    // For SnapKit (Swift)
    func calendar(_ calendar: FSCalendar, boundingRectWillChange bounds: CGRect, animated: Bool) {
        calendar.snp.updateConstraints { (make) in
            make.height.equalTo(bounds.height)
        }
        self.view.layoutIfNeeded()
    }
  3. Handle FSCalendar bounds changes

    master
    When changing scope or switching months (if showsPlaceholders is false), the calendar's bounds may change. You must adjust the frame or constraints in the boundingRectWillChange delegate method to ensure the layout remains correct.
  4. Install FSCalendar via CocoaPods

    master

    To install FSCalendar using CocoaPods, add the following to your Podfile. Note that for iOS 7 compatibility, NSCalendarExtension is required.

    # For iOS8+
    use_frameworks!
    target '<Your Target Name>' do
        pod 'FSCalendar'
    end
    
    # For iOS7+
    target '<Your Target Name>' do
    	pod 'FSCalendar'
    end
  5. Initialize FSCalendar in Objective-C

    master

    You can initialize and add the calendar to your view hierarchy programmatically in Objective-C.

    @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;
  6. Initialize FSCalendar in Swift

    master

    To use FSCalendar in Swift, you must first create a Bridging Header. Note that in Swift 3+, NSDate and NSDateFormatter are renamed to Date and DateFormatter.

    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