我正在尝试更改特定日期的填充颜色,这些日期是当前显示月份的一部分。
我的问题:这仅适用于最初显示的月份。 如果我前后滑动到下一个/上一个月,则背景色似乎分配给了随机日期。
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
}
}
当我前后滑动到上个月/下个月时,我的FSCalendar视图似乎没有正确更新。
任何想法我的代码有什么问题吗?
答案 0 :(得分:0)
此功能解决了我的问题:
func calendarCurrentPageDidChange(_ calendar: FSCalendar) {
calendar.reloadData()
}