Open zeeshanz opened 4 years ago
@zeeshanz did you find a fix for this? I'm dealing with a similar issue.
@TyJPhillips Here is how I am doing it. The 'fix' is the function setMonthButtonsTitle()
private var selectedDate = Date()
private let MONTHS = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
@IBAction func goToPreviousMonthAction(_ sender: Any) {
selectedDate = Calendar.current.date(byAdding: .month, value: -1, to: selectedDate)!
calendarView.goToPreviousMonth()
setMonthButtonsTitle()
}
@IBAction func goToNextMonthAction(_ sender: Any) {
selectedDate = Calendar.current.date(byAdding: .month, value: 1, to: selectedDate)!
calendarView.goToNextMonth()
setMonthButtonsTitle()
}
@IBAction func goToCurrentMonthAction(_ sender: Any) {
selectedDate = Date()
calendarView.setDisplayDate(Date(), animated: true)
setMonthButtonsTitle()
}
private func setMonthButtonsTitle() {
let month = selectedDate.onlyMonth - 1
let prevMonth = (month == 0 ? 11 : month - 1)
let nextMonth = (month == 11 ? 1 : month + 1)
currentMonthButton.setTitle(MONTHS[Date().onlyMonth - 1], for: .normal)
nextMonthButton.setTitle(MONTHS[nextMonth], for: .normal)
previousMonthButton.setTitle(MONTHS[prevMonth], for: .normal)
}
And the onlyMonth
code is in an extension to the Date
like this:
extension Date {
var onlyMonth: Int {
let formatter = DateFormatter(); formatter.dateFormat = "MM"
return Int(formatter.string(from: self as Date))!
}
}
@zeeshanz Thank you so much! I'll see if this helps with my issue!
Any update?
Calls to
self.calendarView.goToPreviousMonth()
,self.calendarView.goToNextMonth()
andself.calendarView.setDisplayDate(Date(), animated: true)
calls protocolfunc headerString(_ date: Date) -> String?
twice and offsets the month by -1 every time, meaning the month is offset twice to -2.The sample code you provide is using an older version of KDCalendar which works fine and doesn't call
headerString()
. But newer version of KDCalendar requires to call it.This is January. I call
goToPreviousMonth()
and it changes the header to February, but then immediately jumps back to January while displaying days of February.Similarly, when I sent it current Date(), it sets header to December while showing days of January.