0

in my app i want something like iOS calendar app so i am using MBCalanderKit library. In my app i don't want to use CKCalendarViewController as i need to add calendar as subview.

For that i am using following code. I am using xCode 7 with swift 2.3

import MBCalendarKit

class ViewController: UIViewController {

    var calanderView : CKCalendarView!
    
    var data = [NSDate : AnyObject]()
    override func viewDidLoad() {
        super.viewDidLoad()
        
        loadData()
        
        calanderView = CKCalendarView(frame: self.view.frame)
        calanderView.autoresizingMask = [.FlexibleWidth,.FlexibleHeight]
        
        calanderView.delegate = self
        calanderView.dataSource = self
        
        
        self.view.addSubview(calanderView)
        
    }
    
    func loadData(){
        let date = NSDate(day: 12, month: 12, year: 2016)
        let event1 = CKCalendarEvent(title: "Birthday Event", andDate: date, andInfo: nil, andColor: UIColor.redColor())
        
        let date2 = NSDate(day: 15, month: 12, year: 2016)
        let event2 = CKCalendarEvent(title: "Party Event", andDate: date2, andInfo: nil, andColor: UIColor.redColor())
        
        let date3 = NSDate(day: 17, month: 12, year: 2016)
        let event3 = CKCalendarEvent(title: "Marriage Event", andDate: date3, andInfo: nil, andColor: UIColor.redColor())
        
        let date4 = NSDate(day: 20, month: 12, year: 2016)
        let event4 = CKCalendarEvent(title: "Splecal Event", andDate: date4, andInfo: nil, andColor: UIColor.redColor())
        
        let date5 = NSDate(day: 25, month: 12, year: 2016)
        let event5 = CKCalendarEvent(title: "Special Event", andDate: date5, andInfo: nil, andColor: UIColor.redColor())
        
        
        data[date] = [event1]
        data[date2] = [event2]
        data[date3] = [event3]
        data[date4] = [event4]
        data[date5] = [event5]

    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

extension ViewController : CKCalendarViewDataSource{
    func calendarView(calendarView: CKCalendarView!, eventsForDate date: NSDate!) -> [AnyObject]! {
        if let dt = date{
            if data[dt] != nil{
                return [self.data[dt]!]
            }
        }
        return nil
    }
}
extension ViewController : CKCalendarViewDelegate{
    
}

But my problem is when i run this code i don't get desired output. Here is my screen shot

enter image description here

Please help me.

Community
  • 1
  • 1
H Raval
  • 1,903
  • 17
  • 39
  • Whats the link for MBCalendar? – Just a coder Dec 22 '16 at 11:45
  • link is https://github.com/MosheBerman/MBCalendarKit – H Raval Dec 22 '16 at 12:07
  • Hmm.. his code is too different to mines. Sorry man, – Just a coder Dec 22 '16 at 19:56
  • can you please suggest me any other swift control which can help me – H Raval Dec 23 '16 at 18:18
  • Of course. You can use the one I created by clicking on [this link](https://github.com/patchthecode/JTAppleCalendar). It is 100% documented and currently the best on Github (in my opinion). If you have any questions on it then you can open an issue on github. I check them every day. Cheers. One other thing. The latest version 6.1 is done in swift 3. But if you must use 2.3, then 5.0.1 is the last version that supports this. Its sad you'd have to use an older version though. 6.1 comes with many updates. – Just a coder Dec 23 '16 at 23:17
  • @JTAppleCalendarforiOSSwift...thnx for replay i have tried ...great efforts but i want something like built in calendar app...i want to show events with dates as monthly, weekly and day view – H Raval Dec 26 '16 at 05:34
  • Alright then. Well i hope the other option works out for you. But just so you know, the job of events is the task of the developer, not the library. – Just a coder Dec 26 '16 at 12:19
  • that i know...i have to dig for it but i am new to IOS development so don't know from where to start...can you please guide me? – H Raval Dec 26 '16 at 13:31

2 Answers2

0

I also gone through the same problem Just make the viewcontroller embedded in navigation controller and add UIView on top of the viewcontroller view make an outlet for the UIView

@IBOutlet var yourAddedView: UIView!

var calendar = CKCalendarView()

override func viewDidLoad() {

    super.viewDidLoad()

    calendar?.dataSource = self
    calendar?.delegate = self

self.yourAddedView.addSubview(calendar!)

}

0

You did everything right. Until recently, MBCalendarKit did not play nicely with Auto Layout. That was a failing on my part, but it was fixed in version 5. You should be able to get this running with the most recent version of MBCalendarKit.

If you still need this and would like to give it a try, you can use something like this code:

func presentCalendar() 
{
  let calendarVC = CalendarViewController()
  calendarVC.dataSource = self
  calendarVC.delegate = self
  self.present(calendarVC, animated:true, completion: nil)
}

Some notes:

  1. In MBCalendarKit 5, the CK prefix was removed from the Swift interoperability headers. (CKCalendarView becomes CalendarView, CKCalenderEvent becomes CalendarEvent, etc.)
  2. In MBCalendarKit 5, you don't get the table view from CalendarView - you need to use CalendarViewController.
  3. Disclosure: I am the author of MBCalendarKit.
Moshe
  • 57,511
  • 78
  • 272
  • 425