0

I'm trying to change the fill color of specific dates, which are part of the currently displayed month.

My problem: this works only for the initially displayed month. If I swipe back or forth to the next/previous month, the background colors seem to be assigned to random dates.

Examples

Initially displayed month (works as expected): Initially displayed month Previous month (wrong color assignment):

Previous month

My View Controller

import Foundation
import FSCalendar

// MARK: - Class: CalendarViewController
class CalendarViewController: UIViewController, FSCalendarDelegate, FSCalendarDelegateAppearance {
    
    // MARK: Instance Properties
    fileprivate lazy var dateFormatter: DateFormatter = {
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy/MM/dd"
        return formatter
    }()
    
    var uncessfulTrainingDays: [String] = []
    var notcompletedTrainingDays: [String] = []
    
    // MARK: IBOutlets
    @IBOutlet private weak var meetingInfoView: UIView!
    @IBOutlet private weak var milestoneReachedLabel: UILabel!
    @IBOutlet private weak var nextMilestoneLabel: UILabel!
    @IBOutlet private weak var calendar: FSCalendar!
    @IBOutlet private weak var nextMeetingDateLabel: UILabel!
    @IBOutlet private weak var nextMeetingDescriptionLabel: UILabel!
    
    // MARK: Overridden Methods
    override func viewDidLoad() {
        super.viewDidLoad()
        uncessfulTrainingDays = ["2019/01/06"]
        notcompletedTrainingDays = []
        meetingInfoView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.meetingPressed)))
        calendar.delegate = self
        calendar.select(Date())
        nextMeetingDateLabel.text = "26.12.2018"
        nextMeetingDescriptionLabel.text = "Meeting: check healing progress"
    }
    
    func calendar(_ calendar: FSCalendar, appearance: FSCalendarAppearance, fillDefaultColorFor date: Date) -> UIColor? {
        let currentDate = Date()
        let key = self.dateFormatter.string(from: date)
        let cal = Calendar.current
        
        // Is date in the curently displayed month?
        let monthOfDate = cal.component(.month, from: date)
        let monthOfCal = cal.component(.month, from: calendar.currentPage)
        
        let isInDisplayedMonth = monthOfDate == monthOfCal
        
        if isInDisplayedMonth {
            if self.uncessfulTrainingDays.contains(key) {
                return UIColor(named: "DarkRed")
            } else if date < currentDate {
                return UIColor(named: "PrimaryAccent")
            }
        }
        return nil
    }
}

It seems that my FSCalendar View is not updating properly when I swipe back or forth to the previous/next month.

Any ideas what could be wrong with my code?

Community
  • 1
  • 1
matthiasunt
  • 709
  • 1
  • 12
  • 32

1 Answers1

1

This function solved my problem:

func calendarCurrentPageDidChange(_ calendar: FSCalendar) {
        calendar.reloadData()
}
matthiasunt
  • 709
  • 1
  • 12
  • 32