0

I have no idea how to use Objective-C in Swift file. I know how to create bridge header file but don't know how to to use it. https://github.com/WenchaoD/FSCalendar. I am using this pods. I want to disable future dates from today and pass a string to subtitle label in calendar. I want to implement this in Swift. I saw a Swift example on github.com but not worked Thanks is advance!

I've also tried this Disable future dates selection in FScalendar swift

if(!isAllowedToLimitFutureDates) 
{
    _maximumDate = [self.formatter dateFromString:@"2099-12-31"];
}
else
{
    _maximumDate = maxValidFutureDateAsString; // say "2017-03-13"
}
Ugnius Malūkas
  • 2,649
  • 7
  • 29
  • 42
Begineers
  • 1
  • 3
  • Just to make sure that I understood your case: you are using `FSCalendar` and you want to disable the highlight of the dates starting from tomorrow, correct? – Ahmad F Apr 25 '18 at 07:54
  • yes that is correct @AhmadF and also want to pass a some string below date label that is in sub title. – Begineers Apr 25 '18 at 08:18
  • These are multiple questions, so the viewer might getting confused about it. I can answer the disabling one :) You might want to separate them later... – Ahmad F Apr 25 '18 at 08:20
  • sure that will work @AhmadF thanks – Begineers Apr 25 '18 at 08:24

1 Answers1

2

In FSCalendar, you could set the maximum date to be selected by conforming to FSCalendarDataSource thus implementing:

maximumDate(for calendar: FSCalendar) -> Date

which tells the calendar view what is the maximum date that allowed to be highlighted. For instance:

Make sure that you are conforming to the delegate:

// conform to FSCalendarDelegate
class ViewController: UIViewController, FSCalendarDataSource {

    override func viewDidLoad() {
        super.viewDidLoad()

        // assign the delegate
        calendar.dataSource = self
    }
}

and then add into your view controller:

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

which means the maximum date is today.

Output:

enter image description here

As you can see, you would be able to select any previous date but not more than today, date starting from tomorrow are grayed (dimmed and unselectable).

Ahmad F
  • 30,560
  • 17
  • 97
  • 143
  • thank you but I get this error now Cannot load module 'fscalendar' as 'FSCalendar' not able to add delegate also – Begineers Apr 25 '18 at 09:08
  • @Begineers have you import it in the .swift file (`import FSCalendar`)? – Ahmad F Apr 25 '18 at 09:11
  • yes I import it then I got errro Cannot load module 'fscalendar' as 'FSCalendar' – Begineers Apr 25 '18 at 09:13
  • @Begineers it could be related to the bridging header. https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html – Ahmad F Apr 25 '18 at 09:14
  • I checked it can you please email me the your project created now at iosbeginners09@gmail.com I can verify it – Begineers Apr 25 '18 at 09:19
  • @Begineers there is something better than send it to your email, open terminal and run `pod try 'FSCalendar'` :) That's what I did, then I edited it. – Ahmad F Apr 25 '18 at 09:21
  • It is saying Updating spec repositories. one more thing have you imported anything in header file created is yes please let me know? – Begineers Apr 25 '18 at 09:26
  • @Begineers No, I didn't. After finishing "Updating spec repositories" it should give you 4 options to select your desired case... – Ahmad F Apr 25 '18 at 09:27
  • Tried but same error "Cannot load module 'fscalendar' as 'FSCalendar" when I import FSCalendar – Begineers Apr 25 '18 at 09:50
  • I got it but future date is not disabled I took uiview reference in file as calendar. class ViewController: UIViewController, FSCalendarDelegate{ @IBOutlet weak var calendar: FSCalendar! override func viewDidLoad() { super.viewDidLoad() calendar.delegate = self } func maximumDate(for calendar: FSCalendar) -> Date { return Date() } } what is calendar in your code is it uiview reference? – Begineers Apr 25 '18 at 10:17
  • @Begineers what's the version of Swift you are using? – Ahmad F Apr 25 '18 at 10:19
  • @DilipTiwari you could get it by implementing `didSelectDate` method from `FSCalendarDelegate`, it contains the selected date (as `Date`). – Ahmad F Apr 02 '19 at 09:30
  • @DilipTiwari I'd assume that it should work... Make sure that you are typing the correct signature for it (it could be it needs "_" before parameter names or something like this). – Ahmad F Apr 02 '19 at 09:35
  • func maximumDate(for calendar: FSCalendar) -> Date { return Date() } and set calendar.delegate = self in viewdidload its not working – Dilip Tiwari Apr 02 '19 at 09:37
  • @DilipTiwari `maximumDate` is for setting the maximum date for the calendar, `didSelectDate` is for selecting the date. What are aiming to achieve?! – Ahmad F Apr 02 '19 at 09:39
  • bro i want to disable future dates only that's why i tried this – Dilip Tiwari Apr 02 '19 at 09:40
  • and i have added two buttons above calendar when i click on Start date i have to select date and when i click on End date i have to select date then it will select all dates automatically between start and end date – Dilip Tiwari Apr 02 '19 at 09:42
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/191076/discussion-between-ahmad-f-and-dilip-tiwari). – Ahmad F Apr 02 '19 at 09:43