1

I've tried to get it to work like so:

class CalendarViewController: DayViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        title = "CalendarKit Demo"
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Dark",
                                                            style: .done,
                                                            target: self,
                                                            action: #selector(changeStyle))
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Change Date",
                                                           style: .plain,
                                                           target: self,
                                                           action: #selector(presentDatePicker))
        navigationController?.navigationBar.isTranslucent = false
        dayView.autoScrollToFirstEvent = true
        reloadData()
    }

    @objc func changeStyle() {
        print("clicked change style")
    }

    @objc func presentDatePicker() {
      print("clicked date picker")
    }

    override func eventsForDate(_ date: Date) -> [EventDescriptor] {
        let models = [Happening(startDate: Date(), endDate: Date(timeInterval: 3600, since: Date()), title: "Test Event", location: "on mother earth")]

      var events = [Event]()

      for model in models {
          let event = Event()
          event.startDate = model.startDate
          event.endDate = model.endDate
          let info = [model.title, model.location]
          event.text = info.reduce("", {$0 + $1 + "\n"})
          events.append(event)
      }
      return events
    }
}

struct Happening {
    let startDate: Date
    let endDate: Date
    let title: String
    let location: String

    init (startDate: Date, endDate: Date, title: String, location: String) {
        self.startDate = startDate
        self.endDate = endDate
        self.title = title
        self.location = location
    }
}

Calendar shows up but I'm neither getting a title nor navigation items. Looks like this for me:

enter image description here

What am I doing wrong here? Many thanks for your help!

Question on the side: Didn't yet figure out how (or if possible at all) to work with it in interface builder, to e.g. add a custom navigation element at the top when integrating it into another app. Is that possible?

Bernhard Engl
  • 243
  • 1
  • 2
  • 12

1 Answers1

0

starting from the last question: the Interface Builder is not currently supported. I recommend creating your CalendarController in code. Some features might work with the Interface Builder, although their support is not guaranteed.

The missing link is that the CalendarController is not embedded inside an instance of the UINavigationController. You can do so in your AppDelegate.swift or SceneDelegate.swift file:

import UIKit
import CalendarKit

@UIApplicationMain
final class AppDelegate: UIResponder, UIApplicationDelegate {
  var window: UIWindow?

  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    window = UIWindow(frame: UIScreen.main.bounds)
    window?.backgroundColor = UIColor.white
    window?.makeKeyAndVisible()

    let dayViewController = CalendarViewController() // Create a view controller
    // Now create a NavigationController with CalendarController embedded inside
    let navigationController = UINavigationController(rootViewController: dayViewController)
    window?.rootViewController = navigationController

    return true
  }
}

Let me know, if this helps you integrate the CalendarKit. Also, I'd appreciate having a reproducible test project, so that I could investigate the problem myself.

Richard Topchii
  • 7,075
  • 8
  • 48
  • 115