How can you make a change to the @Environment(\.calendar) in SwiftUI?

Viewed 377

I would like to change the @Environment(\.calendar) to use calendar.firstWeekday = 2. How can I do that?

2 Answers

You can create your own instance of Calendar and then set it as the environment value

static let myCalendar: Calendar = {
    var calendar = Calendar.current
    calendar.firstWeekday = 2
    return calendar
}()
    

And then when calling the view

SomeView()
   .environment(\.calendar, myCalendar)

Here is what I did:

var body: some Scene {
    WindowGroup {
        ContentView()
            .environment(\.calendar, {
                var calendar = Calendar(identifier: .gregorian)
                calendar.firstWeekday = 2
                return calendar
             }())
        }
    }
}

So all Views in the app get the same calendar.

Related