在 FSCalendar 中设置下一个上一个按钮

Set Next Previous button in FSCalendar

我在我的应用程序中使用 FSCalendar,我想在该日历中显示 next-previous 按钮,以便用户能够也可以点击它们。 试了好几次都没搞定

你可以在 FSCalendar View 上加 2 Button 类似的东西。

这里黄色 View 是我的 FSCalendar 上面有 2 个 button。现在您需要对那 2 button.

采取行动

对于上一个 Button 你需要调用下面的代码。

OBJECTIVE C

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

下一步Button你需要调用下面的代码。

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

SWIFT

对于上一个 Button 你需要调用下面的代码。

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

下一步Button你需要调用下面的代码。

@IBAction func onNextMonthButtonClick(sender: UIButton) {
    self.calendarVW.setCurrentPage(self.dateByAddingMonths(1, currentDate: self.calendarView.currentPage), animated: true)
}

您可以通过以下代码实现,但您需要根据您的设计为下一个和上一个创建自定义按钮。

//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)
}

试试这个代码

@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)!
}

输出