Reputation: 1903
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
Please help me.
Upvotes: 0
Views: 274
Reputation: 58087
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:
CKCalendarView
becomes CalendarView
, CKCalenderEvent
becomes CalendarEvent
, etc.)CalendarView
- you need to use CalendarViewController
. MBCalendarKit
.Upvotes: 0
Reputation:
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!)
}
Upvotes: 0