Fscalendar: Change next and previous month programatically.

Created on 31 May 2016  ·  6Comments  ·  Source: WenchaoD/FSCalendar

Most helpful comment

Add two buttons on top of calendar
declare

var unit: FSCalendarUnit! at top of the controller

Here are following calls to action on buttons

@IBAction func prevTapped(_ sender: Any) {
        print("PREV tapped!")
        unit = (calendarView.scope == FSCalendarScope.month) ? FSCalendarUnit.month : FSCalendarUnit.weekOfYear
        let previousMonth = Calendar.current.date(byAdding: .month, value: -1, to: calendarView.currentPage)
        calendarView.setCurrentPage(previousMonth!, animated: true)
}
@IBAction func nextTapped(_ sender: Any) {
        print("NEXT tapped!")
        unit = (calendarView.scope == FSCalendarScope.month) ? FSCalendarUnit.month : FSCalendarUnit.weekOfYear
        let nextMonth = Calendar.current.date(byAdding: .month, value: 1, to: calendarView.currentPage)
        calendarView.setCurrentPage(nextMonth!, animated: true)
}

All 6 comments

Pls see the demo.

Add two buttons on top of calendar
declare

var unit: FSCalendarUnit! at top of the controller

Here are following calls to action on buttons

@IBAction func prevTapped(_ sender: Any) {
        print("PREV tapped!")
        unit = (calendarView.scope == FSCalendarScope.month) ? FSCalendarUnit.month : FSCalendarUnit.weekOfYear
        let previousMonth = Calendar.current.date(byAdding: .month, value: -1, to: calendarView.currentPage)
        calendarView.setCurrentPage(previousMonth!, animated: true)
}
@IBAction func nextTapped(_ sender: Any) {
        print("NEXT tapped!")
        unit = (calendarView.scope == FSCalendarScope.month) ? FSCalendarUnit.month : FSCalendarUnit.weekOfYear
        let nextMonth = Calendar.current.date(byAdding: .month, value: 1, to: calendarView.currentPage)
        calendarView.setCurrentPage(nextMonth!, animated: true)
}

This works perfect for the month mode, but can you please provide the code for week ?

We can add an extension to FSCalendarScope first to convert this enum into Calendar.Component

extension FSCalendarScope {
    func asCalendarComponent() -> Calendar.Component {
        switch (self) {
        case .month: return .month
        case .week: return .weekOfYear
        }
     }
}

Then we can provide a general function to change calendar:

    private func stepCalendarView(index: Int) {
        let previousMonth = Calendar.current.date(byAdding: self.calendar.scope.asCalendarComponent(), value: index, to: calendar.currentPage)
        calendar.setCurrentPage(previousMonth!, animated: true)
    }

Finally, inside your @IBAction, just use self.stepCalendarView(index: -1) and self.stepCalendarView(index: 1) respectively. :D

How I can add seprate button for month and day in fscalender when i clicked in month next or previous button only month should be swipe and similar for day..

` let startDate: Date
var endDate: Date?

    if self.academicCalendarView.calendarview.scope == .week {

        startDate = self.academicCalendarView.calendarview.currentPage
        let date = self.academicCalendarView.calendarview.selectedDate ?? Date()
        let weekday = self.academicCalendarView.calendarview.gregorian.component(.weekday, from: date)
        endDate = self.academicCalendarView.calendarview.gregorian.date(byAdding: .day, value: weekday - 1, to: startDate)

        self.academicCalendarView.calendarview.select(endDate)

}``
This is the way to select next week date in FSCalendarScope.week

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dippac picture dippac  ·  6Comments

csimmons0 picture csimmons0  ·  4Comments

vishal-iosdeveloper picture vishal-iosdeveloper  ·  5Comments

TomWang1 picture TomWang1  ·  5Comments

ghost picture ghost  ·  5Comments