Skip to content

armstrongnate/CalendarView

Repository files navigation

CalendarView

[![CI Status](http://img.shields.io/travis/Nate Armstrong/CalendarView.svg?style=flat)](https://travis-ci.org/Nate Armstrong/CalendarView) Version License Platform

Usage

To run the example project, clone the repo, and run pod install from the CalendarViewDemo directory first.

Requirements

  • iOS 9.0+
  • Xcode 9.0

Installation

CalendarView is available through Carthage and CocoaPods. To install it, simply add the following line to your Podfile:

pod "CalendarView"

Screenshot

   

Usage

Use the CalendarView class in code:

let calendar = CalendarView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: 320))
view.addSubview(calendar)

or as an outlet (supports auto layout)

@IBOutlet weak var calendar: CalendarView!

Select specific date

The selected date is the current date by default. You can select any date by using the selectDate(date: Moment) method.

let date: Date = YOUR_DATE_HERE
calendar.selectDate(moment(date))

Delegate

A CalendarView's delegate is notified of two events:

calendarDidSelectDate(date: Moment) // called when user taps a date
calendarDidPageToDate(date: Moment) // called when users swipes month

The CalendarView class uses SwiftMoment for date manipulation.

Example use of delegate calls

extension ViewController: CalendarViewDelegate {

  func calendarDidSelectDate(date: Moment) {
    title = date.format(dateFormat: "MMMM d, yyyy")
  }

  func calendarDidPageToDate(date: Moment) {
    title = date.format(dateFormat: "MMMM d, yyyy")
  }

}

Customizations

The aim is to allow the calendar to be as customizable as possible without making it overly complex and bloated.

Appearance

You can customize the look of the calendar by setting certain class properties of CalendarView.

import UIKit
import CalendarView

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

  var window: UIWindow?


  func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Calendar appearance
    CalendarView.daySelectedBackgroundColor = UIColor.orange
    CalendarView.daySelectedTextColor = UIColor.white
    CalendarView.todayBackgroundColor = UIColor(white: 0.0, alpha: 0.3)
    CalendarView.todayTextColor = UIColor.white
    CalendarView.otherMonthBackgroundColor = UIColor.clear
    CalendarView.otherMonthTextColor = UIColor(white: 1.0, alpha: 0.3)
    CalendarView.dayTextColor = UIColor(white: 1.0, alpha: 0.6)
    CalendarView.dayBackgroundColor = UIColor.clear
    CalendarView.weekLabelTextColor = UIColor(white: 1.0, alpha: 0.3)

    return true
  }
}

Selected date on swipe

By default the first day of the month is automatically selected when the user swipes to a different month. You can customize this behavior by modifying the selectedDayOnPaged property of your CalendarView instance:

public var selectedDayOnPaged: Int? = 1

If set to nil, no day will be automatically selected on swipe.

Author

Nate Armstrong, [email protected]

Update to Swift 4

A&D Progress aka verebes, [email protected]

License

CalendarView is available under the MIT license. See the LICENSE file for more info.