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.
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
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)
}
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)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With