airbnb / HorizonCalendar

A declarative, performant, iOS calendar UI component that supports use cases ranging from simple date pickers all the way up to fully-featured calendar apps.
Apache License 2.0
2.81k stars 234 forks source link

Init with selected day #32

Closed thecoolwinter closed 4 years ago

thecoolwinter commented 4 years ago

I'd like to have a CalendarView and a UIDatePicker on the same screen, and be able to initialize the CalendarView with a selected day already provided. Without the user having to select a date.

When trying to create a new Day object I get the error: "'Day' initializer is inaccessible due to 'internal' protection level". So, I tried adding a public init() {} to the Day object with some initial values, but it still is giving the error. The same error happens with the Month object.

I would just make a public init on the Day and Month objects and it would fix my problem, but it's still giving the error.

bryankeller commented 4 years ago

Hi @thecoolwinter - I need an FAQ for this question since it gets asked quite a bit 😛

The short answer - those are intentionally internal to prevent you from making a Day using the wrong Calendar object (which would result in a Day that is incompatible with other Days).

If you want to make a certain day appear selected, you can do so in your dayItemProvider closure. You can convert the provided Day to a Date by doing let date = calendar.date(from: day.dateComponents)!. Then, you can use any of the standard Calendar functions to compare Dates. For example, you could check whether a particular date is "today" and return a different, highlighted calendar item if that condition is true.

More context here: https://github.com/airbnb/HorizonCalendar/pull/28