KickAxe
KickAxe

Reputation: 159

How to import FSCalendar using SwiftUi?

Here is my code:

import SwiftUI
import FSCalendar

class calendars: UIViewController, FSCalendarDelegate{
    var calendar = FSCalendar()

    override func viewDidLoad() {
        super.viewDidLoad()
        calendar.delegate = self
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        calendar.frame = CGRect(x: 0, y: 100, width: view.frame.size.width, height: view.frame.size.width)
        view.addSubview(calendar)
    }
}

struct CalendarView: View {
    var body: some View{
            calendars()
    }
}

I don't really know is it correct to swiftui but I got an error said Return type of property 'body' requires that 'calendars' conform to 'View' I just wanna import a calendar and some kind of to do list.

Upvotes: 1

Views: 1722

Answers (2)

knorrbert
knorrbert

Reputation: 100

The simplest way to include FSCalendar into SwiftUI is to wrap it in an UIView:

struct CalendarView: UIViewRepresentable {

func makeUIView(context: Context) -> UIView {
    return FSCalendar(frame: CGRect(x: 0.0, y: 40.0, width: .infinity, height: 300.0))
}

func updateUIView(_ uiView: UIView, context: Context) {
}
}

Usage in SwiftUI code:

CalendarView()

Upvotes: 3

KickAxe
KickAxe

Reputation: 159

I got it..

struct CalendarController: UIViewControllerRepresentable {

    func makeUIViewController(context: UIViewControllerRepresentableContext<CalendarController>) -> calendars {
        return calendars()
    }

    func updateUIViewController(_ uiViewController: calendars, context: UIViewControllerRepresentableContext<CalendarController>) {
    }

}

Upvotes: 0

Related Questions