Newbie SwiftUI Dev here. I want to create a scheduling app in SwiftUI and I would like to create a button in navigation bar which change calendar's scope. From .week to month and return.
struct HomeVC: View {
init() {
navbarcolor.configureWithOpaqueBackground()
navbarcolor.backgroundColor = .systemGreen
navbarcolor.titleTextAttributes = [.foregroundColor: UIColor.white]
navbarcolor.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
UINavigationBar.appearance().standardAppearance = navbarcolor
UINavigationBar.appearance().scrollEdgeAppearance = navbarcolor
}
@State private var selectedDate = Date()
var body: some View {
NavigationView{
VStack{
CalendarRepresentable(selectedDate: $selectedDate)
.frame(height: 300)
.padding(.top, 15)
Spacer()
ListView()
}
.navigationBarTitle("Calendar")
.toolbar {
Button(action: {
switchCalendarScope()
}) {
Text("Toggle")
}
}
}
}
}
This is my calendar struct, and I would like to take from here the switchCalendarScope function, and use it into button's action, but doesn't work.
struct CalendarRepresentable: UIViewRepresentable{
typealias UIViewType = FSCalendar
@Binding var selectedDate: Date
var calendar = FSCalendar()
func switchCalendarScope(){
if calendar.scope == FSCalendarScope.month {
calendar.scope = FSCalendarScope.week
} else {
calendar.scope = FSCalendarScope.month
}
}
func updateUIView(_ uiView: FSCalendar, context: Context) { }
func makeUIView(context: Context) -> FSCalendar {
calendar.delegate = context.coordinator
calendar.dataSource = context.coordinator
calendar.allowsMultipleSelection = true
calendar.scrollDirection = .vertical
calendar.scope = .week
//:Customization
calendar.appearance.headerTitleFont = UIFont.systemFont(ofSize: 25, weight: UIFont.Weight.heavy)
calendar.appearance.weekdayFont = .boldSystemFont(ofSize: 15)
calendar.appearance.weekdayTextColor = .black
calendar.appearance.selectionColor = .systemGreen
calendar.appearance.todayColor = .systemGreen
calendar.appearance.caseOptions = [.headerUsesUpperCase, .weekdayUsesUpperCase]
calendar.appearance.headerTitleColor = .black
return calendar
}
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
class Coordinator: NSObject, FSCalendarDelegate, FSCalendarDataSource {
var parent: CalendarRepresentable
var formatter = DateFormatter()
init(_ parent: CalendarRepresentable) {
self.parent = parent
}
func calendar(_ calendar: FSCalendar, numberOfEventsFor date: Date) -> Int {
return 0
}
func calendar(_ calendar: FSCalendar, didSelect date: Date, at monthPosition: FSCalendarMonthPosition) {
formatter.dateFormat = "dd-MM-YYYY"
print("Did select == \(formatter.string(from: date))")
}
func calendar(_ calendar: FSCalendar, didDeselect date: Date, at monthPosition: FSCalendarMonthPosition) {
formatter.dateFormat = "dd-MM-YYYY"
print("Did de-select == \(formatter.string(from: date))")
}
}
}
Can anybody help?