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.
Asked
Active
Viewed 5,388 times
7
-
do 1 thing add 2 `button` over `FSCalendar` `View`. and give action to that `buttons`. – Kuldeep Apr 24 '18 at 07:13
3 Answers
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
4
You can add 2 Button
over FSCalendar
View
something like that.
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
-
Thanks a lot for ur precious help,I have implemented the same way as u told,but is there any way to get buttons from FSCalendar itself? – priyanshu Apr 24 '18 at 08:13
-
-
-
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