如何更改 FSCalendar 中的月份?

How to change month in FSCalendar?

我正在使用 FSCalendar,我想根据我的要求对其进行自定义。所以,我在视图控制器中有一个月份列表,同一个控制器有 FSCalendar。我想要的是,当我点击任何特定月份时,日历仅更改为该月份。

月份列表在集合视图中。单击我显示上个月的单元格,但我不知道如何显示任何特定月份。

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)
        }
    }

我用谷歌搜索了很多,但还没有得到任何答案。这可以用 FSCalendar 实现吗?

首先,找到当前月份并从所选单元格索引中减去。像这样。

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)

我使用下个月和上个月的按钮更改月份。我想这可能对你有用。

下一个

let currentPage = self.fsCalendarView.currentPage
self.fsCalendarView.setCurrentPage(currentPage.addingComponentsOfDate(year: 0, month: 1, day: 0)!, animated: true)

上一篇

let currentPage = self.fsCalendarView.currentPage
self.fsCalendarView.setCurrentPage(currentPage.addingComponentsOfDate(year: 0, month: -1, day: 0)!, animated: true)