iosswiftfscalendar

How to change month in FSCalendar?


I am working with FSCalendar and I want to customize it according to my requirement. So, I have a list of months in a view controller and the same controller has FSCalendar. What I want is that when I click on any particular month the calender changes to that month only.

The list of months is in a collectionview. On clicking the cell I am showing the previous month but I don't how to show any particular month.

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if let currentCell = collectionView.cellForItem(at: indexPath) as? AttendenceMonthCell {
          // what code should I write here to get the particular month     
            let month = Calendar.current.date(byAdding: .month, value: -1, to: calender.currentPage)
            calender.setCurrentPage(month!, animated: true)
        }
    }

I have googled alot but haven't got any answers yet. Is this possible with FSCalendar?


Solution

  • First, find the current month and subtract from the selected cell index. Like this.

    let index = indexPath.item + 1
    let currentMonth = Calendar.current.component(.month, from: Date())
    
    let month = Calendar.current.date(byAdding: .month, value: index - currentMonth, to: calender.currentPage)
    calender.setCurrentPage(month!, animated: true)