FSCalendar in iOS Swift

Save

FSCalendaris a fully customizable iOS calendar library. So its property is fully useful.

It is an open-source iOS calendar. It supports horizontal, portrait sliding mode, and other functions.

 

 

To successfully integrate the FSCalendar we have to follow the following steps:-

Step To Integrate FSCalendar

Step 1:Add ‘FSCalendar’ In The Project.

Before we start first integrate FSCalendar into your Xcode project using CocoaPods.

 

Step 2:- Now follows the below points.

(i) Drag an UIView object to ViewController Scene.

(ii) Change the Custom Class to FSCalendar.

After following the above two points. It will look like the below-mentioned screenshot.

 

Step 3:- Next part is it’s ‘FSCalendarDataSource‘ and  ‘FSCalendarDelegate’.

Add FSCalendarDataSourceand  ‘FSCalendarDelegate’ in your view controller. So that we can use its delegates and data source methods.

Now we will look into this.

1) First, we see FSCalendardataSource.

The following  ‘FSCalendardataSource’ method is:-

             ==> Asks the dataSource for a title for the specific date.

             ==> Asks the dataSource for a subtitle.

             ==> Asks the dataSource for an image for the specific date.

             ==> Asks the dataSource the minimum date to display.

             ==> Asks the dataSource the maximum date to display.

            ==> Asks the data source for a cell to insert in a particular data of the calendar.

Let see some Example.

The following method is:-

Before using cellfor we first have to register the cell. After that, we can use cellFor date.

The following method is:-

2) Now, we see FSCalendarDelegate

The following method is:-

            ==> It specifies whether date is allowed to be selected by tapping.

          ==> Tells the delegate the calendar is about to change the current page.

Now we see some examples of ‘FSCalendarDelegate’

The following method is:-

 

Conclusion

 

So please follow the above step to integrate FSCalendar, and if you have any issue or suggestion you can leave your query/suggestion in the comment section.

. . .
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


6 comments

  • Lokesh Patil
    Hii,
    1) I am facing a issue with calander when i call did tab method its returns me one day before date like if i click on 6 dec it gives me 5 dec
    2) I need to show blur date which is not selectable for this what should i do.
    3) I need an array of multiple dates if i select multiple dates.
    • Vicky Kumar (Moderator)
      1) I am facing a issue with calander when i call did tab method its returns me one day before date like if i click on 6 dec it gives me 5 dec

      ==> Use the below date extension

      extension Date {
      // Convert UTC (or GMT) to local time

      func toLocalTime() -> Date {

      let timezone = TimeZone.current

      let seconds = TimeInterval(timezone.secondsFromGMT(for: self))

      return Date(timeInterval: seconds, since: self)

      }

      // Convert local time to UTC (or GMT)

      func toGlobalTime() -> Date {

      let timezone = TimeZone.current

      let seconds = -TimeInterval(timezone.secondsFromGMT(for: self))

      return Date(timeInterval: seconds, since: self)

      }

      }

      And in didSelect method use like => date.toLocalTime()

      2) I need to show blur date which is not selectable for this what should i do.
      ==> Check below example

      func calendar(_ calendar: FSCalendar, cellFor date: Date, at position: FSCalendarMonthPosition) -> FSCalendarCell {
      let cell: FSCalendarCell = calendar.dequeueReusableCell(withIdentifier: “CELL”, for: date, at: position)
      let dateFromStringFormatter = DateFormatter()
      dateFromStringFormatter.dateFormat = “YYYY-MM-dd”
      let calendarDate = dateFromStringFormatter.string(from: date)
      let weekDays = getDayOfWeek(calendarDate) ?? “”
      var weekToBedisable: Bool = false
      if disabledays?.contains(weekDays) ?? false{
      weekToBedisable = true
      }

      if disableDates?.contains(calendarDate) ?? false || weekToBedisable{
      cell.isUserInteractionEnabled = false
      }else{
      cell.isUserInteractionEnabled = true

      }
      return cell

      }

      func calendar(_ calendar: FSCalendar, appearance: FSCalendarAppearance, titleDefaultColorFor date: Date) -> UIColor? {
      let dateFromStringFormatter = DateFormatter()
      dateFromStringFormatter.dateFormat = “YYYY-MM-dd”
      let calendarDate = dateFromStringFormatter.string(from: date)
      var weekToBedisable: Bool = false
      let weekDays = getDayOfWeek(calendarDate) ?? “”
      if disabledays?.contains(weekDays) ?? false{
      weekToBedisable = true
      }
      if disableDates?.contains(calendarDate) ?? false || weekToBedisable{
      return .lightGray
      }else{
      return nil
      }
      }

      func getDayOfWeek(_ today:String)->String? {
      let formatter = DateFormatter()
      formatter.dateFormat = “YYYY-MM-dd”
      guard let todayDate = formatter.date(from: today) else { return nil}
      let myCalendar = Calendar(identifier: .gregorian)
      let weekDay = myCalendar.component(.weekday, from: todayDate)
      return String(weekDay)
      }

      3) I need an array of multiple dates if i select multiple dates.
      ==> Enable MultipleSelction
      bookingCalendar.allowsMultipleSelection = true

  • ortal
    Hi,
    I’m having some issues with the code, not quite understand where should I put this line bookingCalendar.register(FSCalendarCell.self, forCellReuseIdentifier: “CELL”),
    sorry for the question i’m a beginner
    • Vicky Kumar (Moderator)
      You have to add in the same viewController in which you are using FSCalendar.
  • Ummer akbar
    getting below exception
    Terminating app due to uncaught exception ‘The cell class must be a subclass of FSCalendarCell.’, reason: ”
    • Vicky Kumar (Moderator)
      You have to also register the FSCalendarCell in the viewDidLoad

      bookingCalendar.register(FSCalendarCell.self, forCellReuseIdentifier: “CELL”)

  • css.php

    Webkul is a truly trusted and supported IT Company to develop business ideas for e-commerce. They provide professional support after deploying the solution to production and be responsible to act for fixing the reported issues or system errors. We highly encourage to deal with Webkul for business development.

    Hussein Zawia

    Hussein Zawia

    Co-founder, 24Dokan

    Talk to Sales

    Global

    Live Chat
    Start a Project


      Message Sent!

      If you have more details or questions, you can reply to the received confirmation email.

      Back to Home