Disable future dates selection in FScalendar swift

旧城冷巷雨未停 提交于 2019-12-01 09:39:00

问题


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


回答1:


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!




回答2:


You should be using the delegate method to address this

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



回答3:


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




回答4:


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
  }


来源:https://stackoverflow.com/questions/42760793/disable-future-dates-selection-in-fscalendar-swift

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