Sreekanth
Sreekanth

Reputation: 549

Disable future dates selection in FScalendar swift

I am using https://github.com/WenchaoD/FSCalendar in my project . MaximumSelectedDate is a read-only property .Then how can disable future dates ?

Upvotes: 4

Views: 7078

Answers (4)

Joseph Mikko Manoza
Joseph Mikko Manoza

Reputation: 296

for Swift 3

  fileprivate lazy var dateFormatter2: DateFormatter = {
  let formatter = DateFormatter()
  formatter.dateFormat = "dd-MM-yyyy"
  return formatter }()       

  let today   = dateFormatter2.string(from: calendar.selectedDate!)
  let dateObj = dateFormatter2.date(from: today)

  if dateObj! > calendar.today! {
     // Your logic here
  }

Upvotes: 0

Mauricio Chirino
Mauricio Chirino

Reputation: 1232

@Devraj answer is correct, there are delegates for both minimum and maximum dates, all you need to do is implementing the proper one (the later one in your case) in the controller that's conforming to FSCalendarDelegate and that'll do the trick.

func maximumDateForCalendar(calendar: FSCalendar) -> NSDate { return NSDate() // NSDate of your choosing here }

Upvotes: 0

Devraj
Devraj

Reputation: 3065

You should be using the delegate method to address this

func maximumDate(for calendar: FSCalendar) -> Date {
    return Date()
}

Upvotes: 15

NeverHopeless
NeverHopeless

Reputation: 11233

A workaround could be to edit FSCalendar method file. First make a bool variable, say isAllowedToLimitFutureDates and a string variable maxValidFutureDateAsString then change line 172 of this link to:

 if(!isAllowedToLimitFutureDates)
 {
     _maximumDate = [self.formatter dateFromString:@"2099-12-31"];
 }
 else
 {
     _maximumDate = maxValidFutureDateAsString; // say "2017-03-13"
 }

So when you want to limit the dates set isAllowedToLimitFutureDates = true.

Similar approach to line 1707.

In case you cannot edit file and used PODs, then you can customize this control and override them.

Hope that helps!

Upvotes: 1

Related Questions