mmick66 / CalendarView

An Easy to Use Calendar for iOS (Swift 5.0)
MIT License
595 stars 114 forks source link

headerString(_ date: Date) offsets month by -1 #109

Open zeeshanz opened 4 years ago

zeeshanz commented 4 years ago

Calls to self.calendarView.goToPreviousMonth(), self.calendarView.goToNextMonth() and self.calendarView.setDisplayDate(Date(), animated: true) calls protocol func 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.

func headerString(_ date: Date) -> String? {
    print("Selected Date is: \(date.onlyMonth)")
    return date.onlyMonth // onlyMonth is I extension which grabs the month portion of the date
}

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.

wrong_month wrong_month3 wrong_month2

tys-adventure commented 4 years ago

@zeeshanz did you find a fix for this? I'm dealing with a similar issue.

zeeshanz commented 4 years ago

@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))!
    }
}
tys-adventure commented 4 years ago

@zeeshanz Thank you so much! I'll see if this helps with my issue!

salmankhalid876 commented 1 year ago

Any update?