Disable future dates selection in FScalendar swift

情到浓时终转凉″ 提交于 2019-12-01 11:14:43

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!

You should be using the delegate method to address this

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

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

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
  }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!