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

时间:2018-04-24 06:32:44

标签: ios swift fscalendar

我在我的应用中使用 FSCalendar ,我想在该日历中显示 next-previous 按钮,以便用户也可以点按它们。 努力尝试但没有任何想法。

3 个答案:

答案 0 :(得分:1)

您可以在Button FSCalendar之上添加2 Viewenter image description here

此处黄色View是我的FSCalendar,其中有2 button。现在你需要对2 button

采取行动

对于上一个Button,您需要调用以下代码。

目标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)
}

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

试试这段代码

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

输出

enter image description here