1

我试图让它像这样工作:

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
    }
}

日历显示,但我既没有得到标题也没有导航项目。对我来说看起来像这样:

在此处输入图像描述

我在这里做错了什么?非常感谢您的帮助!

侧面的问题:尚未弄清楚如何(或者如果可能的话)在界面构建器中使用它,例如在将其集成到另一个应用程序时在顶部添加自定义导航元素。那可能吗?

4

1 回答 1

0

从最后一个问题开始:目前不支持Interface Builder。我建议创建您CalendarController的代码。某些功能可能适用于 Interface Builder,但不能保证它们的支持。

缺少的链接是CalendarController没有嵌入到UINavigationController. 您可以在您的AppDelegate.swiftorSceneDelegate.swift文件中执行此操作:

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
  }
}

如果这有助于您集成 CalendarKit,请告诉我。另外,我希望有一个可重现的测试项目,这样我就可以自己调查问题。

于 2020-03-15T21:19:14.727 回答