0

When I used next and previous button for both month and day when i clicked on month next button it should be swipe only month not day and same as for day please help me...how can I do I am using this code but when I clicked next or previous button both month and dates are swipping I want individual

  @IBAction func year_leftbtnAction(_ sender: Any)
{
    print("Clicked")

    calendar.setCurrentPage(getPreviousMonth(date: calendar.currentPage), animated: true)
}

func getPreviousMonth(date:Date)->Date
{
    return  Calendar.current.date(byAdding: .weekOfMonth, value: -1, to:date)!
}


@IBAction func year_rightbtnAction(_ sender: Any)
{
    print("Clicked")

    calendar.setCurrentPage(getNextMonth(date: calendar.currentPage), animated: true)
}

func getNextMonth(date:Date)->Date
{
    return  Calendar.current.date(byAdding: .weekOfMonth, value: 1, to:date)!
}

1 Answer 1

1

You can use this function to switch any date component in FSCalendar 2.8.0 (with Swift 4.2/Xcode 10):

func switchDateComponent(component: Calendar.Component, isNextDirection: Bool) {
    if let nextDate = Calendar.current.date(byAdding: component, value: isNextDirection ? 1 : -1, to: calendar.selectedDate ?? Date()) {
        calendar.select(nextDate, scrollToDate: true)
    }
}

Now if you need to select previous/next day you can call switchDateComponent(component: .day, isNextDirection: false) or switchDateComponent(component: .day, isNextDirection: true). For the month component it will be similar, just call switchDateComponent(component: .month, isNextDirection: false) or switchDateComponent(component: .month, isNextDirection: true).

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.