7

I m using FSCalendar in my app,i want to show next-previous buttons in that calendar so that user can be able to tap them as well. Tried hard but not getting any idea.

priyanshu
  • 155
  • 2
  • 11

3 Answers3

8

try this code

@IBOutlet weak var calendar: FSCalendar!
....
@IBAction func nextTapped(_ sender:UIButton) {
    calendar.setCurrentPage(getNextMonth(date: calendar.currentPage), animated: true)
}

@IBAction  func previousTapped(_ sender:UIButton) {
    calendar.setCurrentPage(getPreviousMonth(date: calendar.currentPage), animated: true)
}

func getNextMonth(date:Date)->Date {
    return  Calendar.current.date(byAdding: .month, value: 1, to:date)!
}

func getPreviousMonth(date:Date)->Date {
    return  Calendar.current.date(byAdding: .month, value: -1, to:date)!
}

output

enter image description here

Kuldeep
  • 4,466
  • 8
  • 32
  • 59
a.masri
  • 2,439
  • 1
  • 14
  • 32
4

You can add 2 Button over FSCalendar View something like that. enter image description here

Here Yellow colour View is my FSCalendar and there's 2 button over it. Now you need to give action to that 2 button.

For Previous Button you need to call below code.

OBJECTIVE C

- (IBAction)onPreviousMonthButtonClick:(UIButton *)sender {
    [self.calendarVW setCurrentPage:[self.gregorian dateByAddingUnit:NSCalendarUnitMonth value:-1 toDate:self.calendarVW.currentPage options:0] animated:YES];
}

For Next Button you need to call below code.

- (IBAction)onNextMonthButtonClick:(UIButton *)sender {
    [self.calendarVW setCurrentPage:[self.gregorian dateByAddingUnit:NSCalendarUnitMonth value:1 toDate:self.calendarVW.currentPage options:0] animated:YES];
}

SWIFT

For Previous Button you need to call below code.

@IBAction func onPreviousMonthButtonClick(sender: UIButton) {
    self.calendarVW.setCurrentPage(self.gregorian.date(byAdding: .month, value: -1, to: self.calendarVW.currentPage), animated: true)
}

For Next Button you need to call below code.

@IBAction func onNextMonthButtonClick(sender: UIButton) {
    self.calendarVW.setCurrentPage(self.dateByAddingMonths(1, currentDate: self.calendarView.currentPage), animated: true)
}
Kuldeep
  • 4,466
  • 8
  • 32
  • 59
1

You can achieve it by the following code but you need to create custom buttons for Next and Previous as per your design.

//MARK: - Next Button Action -
@IBAction func btnNextMonthClicked(sender: UIButton) {

    let currentDate = self.calendarView.currentPage
    let date = self.dateByAddingMonths(1, currentDate: currentDate)
    self.calendarView.setCurrentPage(date, animated: true)
}

// For getting Next month 
func dateByAddingMonths(dMonths: Int, currentDate: NSDate) -> NSDate{

    let dateComponent = NSDateComponents()
    dateComponent.month = dMonths
    let newDate = NSCalendar.currentCalendar().dateByAddingComponents(dateComponent, toDate: currentDate, options: NSCalendarOptions.WrapComponents)
    return newDate!
}

//MARK: - Previous Button Action -
@IBAction func btnPreviousMonthClicked(sender: AnyObject) {

    let currentDate = self.calendarView.currentPage
    let date = self.dateBySubtractingMonths(1, currentDate: currentDate)
    self.calendarView.setCurrentPage(date, animated: true)
}

// For Previous Month
func dateBySubtractingMonths(dMonths: Int, currentDate: NSDate) -> NSDate{

    return self.dateByAddingMonths(-dMonths, currentDate: currentDate)
}
iOS Developer
  • 538
  • 4
  • 10