Calendar

Calendars are used for date selection within a defined time period.

Backpack/Calendar

Cocoapods class reference view on Github

Single date

Day Night

Date range

Day Night

Multiple

Day Night

Custom colours

Day Night

With prices

Day Night

Showing year pill

Day Night

Installation

In Podfile add

pod 'Backpack/Calendar'

and then run pod install.

Usage

Backpack/Calendar contains the Backpack Calendar component in the class BPKCalendar. The calendar is a calendar view offering user interaction.

Simple calendar

Objective-C


                                                
                                                #import <Backpack/Calendar.h>
                                                #import <Backpack/SimpleDate.h>
                                                
                                                BPKCalendar *bpkCalendar = [[BPKCalendar alloc] initWithFrame:CGRectZero];
                                                
                                                bpkCalendar.selectionConfiguration = [[BPKCalendarSelectionConfigurationSingle alloc] initWithSelectionHint:@"Double tap to select date"];
                                                bpkCalendar.selectedDates = @[[bpkCalendar simpleDateFromDate:self.date1]];
                                                [bpkCalendar reloadData];
                                                
                                                // ...
                                                
                                                #pragma mark - <BPKCalendarDelegate>
                                                
                                                - (void)calendar:(BPKCalendar *)calendar didChangeDateSelection:(NSArray<BPKSimpleDate *> *)dateList {
                                                    // do stuff
                                                }
                                                
                                                - (void)calendar:(BPKCalendar *)calendar didScroll:(CGPoint)contentOffset {
                                                    // do stuff
                                                }
                                                
                                                - (BOOL)calendar:(BPKCalendar *)calendar isDateEnabled:(NSDate *)date {
                                                    // do stuff
                                                }

Swift


                                                
                                                import Backpack
                                                
                                                let calendar = BPKCalendar(frame: .zero)
                                                calendar.minDate = calendar.simpleDate(from: Date())
                                                calendar.locale = Locale.current
                                                
                                                // ...
                                                
                                                extension MyClass: BPKCalendarDelegate {
                                                    func calendar(_ calendar: BPKCalendar, didChangeDateSelection dateList: [BPKSimpleDate]) {
                                                        // do stuff
                                                    }
                                                
                                                    func calendar(_ calendar: BPKCalendar, didScroll contentOffset: CGPoint) {
                                                        // do stuff
                                                    }
                                                
                                                    func calendar(_ calendar: BPKCalendar, isDateEnabled: Date) -> Bool {
                                                        // do stuff
                                                    }
                                                }

Color-coded calendar

A colour coded calendar where dates are coloured based on how expensive/cheap they are.

Objective-C


                                                
                                                #import <Backpack/Calendar.h>
                                                #import <Backpack/SimpleDate.h>
                                                
                                                NSSet<BPKSimpleDate *> *lowPrices = /* .... */;
                                                NSSet<BPKSimpleDate *> *mediumPrices = /* .... */;
                                                NSSet<BPKSimpleDate *> *highPrices = /* .... */;
                                                
                                                BPKCalendar *bpkCalendar = [[BPKCalendar alloc] initWithFrame:CGRectZero];
                                                
                                                bpkCalendar.selectionConfiguration = [[BPKCalendarSelectionConfigurationSingle alloc] initWithSelectionHint:@"Double tap to select date"];
                                                bpkCalendar.selectedDates = @[[bpkCalendar simpleDateFromDate:self.date1]];
                                                [bpkCalendar reloadData];
                                                
                                                // ...
                                                
                                                #pragma mark - <BPKCalendarDelegate>
                                                
                                                - (id _Nullable)calendar:(BPKCalendar *)calendar cellDataForDate:(BPKSimpleDate *)date {
                                                    if ([lowPrices containsObject:date) {
                                                        return BPKCalendarTrafficLightCellData.positive;
                                                    }
                                                
                                                    if ([mediumPrices containsObject:date) {
                                                        return BPKCalendarTrafficLightCellData.neutral;
                                                    }
                                                
                                                    if ([highPrices containsObject:date) {
                                                        return BPKCalendarTrafficLightCellData.negative;
                                                    }
                                                
                                                    return BPKCalendarTrafficLightCellData.normal;
                                                }
                                                
                                                - (void)calendar:(BPKCalendar *)calendar didChangeDateSelection:(NSArray<BPKSimpleDate *> *)dateList {
                                                    // do stuff
                                                }

Swift


                                                
                                                import Backpack
                                                
                                                let lowPrices: Set<BPKSimpleDate> = ...
                                                let mediumPrices: Set<BPKSimpleDate> = ...
                                                let highPrices: Set<BPKSimpleDate> = ...
                                                
                                                let calendar = BPKCalendar(frame: .zero)
                                                calendar.minDate = calendar.simpleDate(from: Date())
                                                calendar.locale = Locale.current
                                                
                                                // ...
                                                
                                                extension MyClass: BPKCalendarDelegate {
                                                  func calendar(_ calendar: BPKCalendar, cellDataFor date: BPKSimpleDate) -> Any? {
                                                      if lowPrices.contains(date) {
                                                          return BPKCalendarTrafficLightCellData.positive
                                                      }
                                                
                                                      if mediumPrices.contains(date) {
                                                          return BPKCalendarTrafficLightCellData.neutral
                                                      }
                                                
                                                      if highPrices.contains(date) {
                                                          return BPKCalendarTrafficLightCellData.negative
                                                      }
                                                
                                                      return BPKCalendarTrafficLightCellData.normal
                                                  }
                                                
                                                  }
                                                  func calendar(_ calendar: BPKCalendar, didChangeDateSelection dateList: [BPKSimpleDate]) {
                                                      // do stuff
                                                  }
                                                }

Priced calendar

A calendar where cells display a price.

Objective-C


                                                
                                                #import <Backpack/Calendar.h>
                                                #import <Backpack/SimpleDate.h>
                                                
                                                NSSet<BPKSimpleDate *> *lowPrices = /* .... */;
                                                NSSet<BPKSimpleDate *> *mediumPrices = /* .... */;
                                                NSSet<BPKSimpleDate *> *highPrices = /* .... */;
                                                
                                                BPKCalendar *bpkCalendar = [[BPKCalendar alloc] initWithConfiguration: [BPKCalendarPriceLabelConfiguration new]];
                                                
                                                bpkCalendar.selectionConfiguration = [[BPKCalendarSelectionConfigurationSingle alloc] initWithSelectionHint:@"Double tap to select date"];
                                                bpkCalendar.selectedDates = @[[bpkCalendar simpleDateFromDate:self.date1]];
                                                [bpkCalendar reloadData];
                                                
                                                // ...
                                                
                                                #pragma mark - <BPKCalendarDelegate>
                                                
                                                - (id _Nullable)calendar:(BPKCalendar *)calendar cellDataForDate:(BPKSimpleDate *)date {
                                                    if ([lowPrices containsObject:date) {
                                                        return [[BPKCalendarPriceLabelCellData alloc] initWithPrice: @"£12" labelStyle: BPKCalendarPriceLabelStyle.positive];
                                                    }
                                                
                                                    if ([mediumPrices containsObject:date) {
                                                        return [[BPKCalendarPriceLabelCellData alloc] initWithPrice: @"£25" labelStyle: BPKCalendarPriceLabelStyle.neutral];
                                                    }
                                                
                                                    if ([highPrices containsObject:date) {
                                                        return [[BPKCalendarPriceLabelCellData alloc] initWithPrice: @"£43" labelStyle: BPKCalendarPriceLabelStyle.negative];
                                                    }
                                                
                                                    return [[BPKCalendarPriceLabelCellData alloc] initWithPrice: @"-" labelStyle: BPKCalendarPriceLabelStyle.noData];
                                                }
                                                
                                                - (void)calendar:(BPKCalendar *)calendar didChangeDateSelection:(NSArray<BPKSimpleDate *> *)dateList {
                                                    // do stuff
                                                }

Swift


                                                
                                                import Backpack
                                                
                                                let lowPrices: Set<BPKSimpleDate> = ...
                                                let mediumPrices: Set<BPKSimpleDate> = ...
                                                let highPrices: Set<BPKSimpleDate> = ...
                                                
                                                let calendar = BPKCalendar(configuration: BPKCalendarPriceLabelConfiguration())
                                                calendar.minDate = calendar.simpleDate(from: Date())
                                                calendar.locale = Locale.current
                                                
                                                // ...
                                                
                                                extension MyClass: BPKCalendarDelegate {
                                                  func calendar(_ calendar: BPKCalendar, cellDataFor date: BPKSimpleDate) -> Any? {
                                                      if lowPrices.contains(date) {
                                                          return BPKCalendarPriceLabelCellData(price: "£12", labelStyle: BPKCalendarPriceLabelStyle.positive)
                                                      }
                                                
                                                      if mediumPrices.contains(date) {
                                                          return BPKCalendarPriceLabelCellData(price: "£25", labelStyle: BPKCalendarPriceLabelStyle.neutral)
                                                      }
                                                
                                                      if highPrices.contains(date) {
                                                          return BPKCalendarPriceLabelCellData(price: "£43", labelStyle: BPKCalendarPriceLabelStyle.negative)
                                                      }
                                                
                                                      return BPKCalendarPriceLabelCellData(price: "-", labelStyle: BPKCalendarPriceLabelStyle.noData)
                                                  }
                                                
                                                  }
                                                  func calendar(_ calendar: BPKCalendar, didChangeDateSelection dateList: [BPKSimpleDate]) {
                                                      // do stuff
                                                  }
                                                }

Whole month selection

You can enable the selection of an entire month by setting the wholeMonthSelectionConfiguration on the BPKCalendar. The selectableMonthRange can be nil, which will not disable any selection options.

Keep in mind that when you set a min and max date on the calendar, you want to match this in your whole month configuration.

There are no safeguards in place to disable whole month for months that are fully disabled.


                                                
                                                let calendar = BPKCalendar()
                                                /* code omitted */
                                                calendar.wholeMonthSelectionConfiguration = BPKCalendarWholeMonthConfiguration(
                                                    title: "Select whole month",
                                                    selectableMonthRange: BPKYearMonth(year: 2020, month: 1)...BPKYearMonth(year: 2020, month: 12)
                                                )

Appearance attributes

  • (UIColor)dateSelectedContentColor
  • (UIColor)dateSelectedBackgroundColor