Disable future dates selection in FScalendar swift

2019-02-25 17:17发布

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

4条回答
成全新的幸福
2楼-- · 2019-02-25 17:38

You should be using the delegate method to address this

func maximumDate(for calendar: FSCalendar) -> Date {
    return Date()
}
查看更多
一纸荒年 Trace。
3楼-- · 2019-02-25 17:52

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!

查看更多
爷的心禁止访问
4楼-- · 2019-02-25 17:54

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
  }
查看更多
在下西门庆
5楼-- · 2019-02-25 18:02

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

查看更多
登录 后发表回答