Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Next Previous button in FSCalendar

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.

like image 522
priyanshu Avatar asked Apr 24 '18 06:04

priyanshu


3 Answers

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

like image 144
a.masri Avatar answered Nov 09 '22 02:11

a.masri


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)
}
like image 3
Kuldeep Avatar answered Nov 09 '22 00:11

Kuldeep


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)
}
like image 1
iOS Developer Avatar answered Nov 09 '22 02:11

iOS Developer