UICalendarView iOS16 In Swift

Shivam Thaker
2 min readJun 13, 2022

It is WWDC22 weak… After lots of experience in iOS field but on calendar & Calendar Ui customisation takes our lots of time. you’ll always look it up how to set up a date formatter. Calendar are confusing.. How set next date 😒. Set note & event 🤯.

So many things latest in iOS16. New feature in iOS16.

Apple introduced UICalendarView to create custom calendar views from iOS 16. ✌

UICalendarView in iOS 16 A view that displays a calendar with date-specific decorations, and provides for user selection of a single or multiple dates.

@MainActor class UICalendarView : UIView

Setting up UICalendarView

What’s latest in iOS 16. Calendar update in iOS 16.

UICalendarView belongs to the UIKit framework and comes with a simple initialiser.

let calendarView = UICalendarView()
let gregorianCalendar = Calendar(identifier: .gregorian)
calendarView.calendar = gregorianCalendar
calendarView.locale = Locale(identifier: "zh_TW")
calendarView.fontDesign = .rounded

Easy to customised UICalendarView

calendarView.backgroundColor = .secondarySystemBackground calendarView.layer.cornerCurve = .continuous calendarView.layer.cornerRadius = 10.0 calendarView.tintColor = UIColor.systemTeal

UICalendarView declaration

@MainActor class UICalendarView : UIView

I have added a yellow star image to all the dates in the calendar. You can write your logic to customised the decorators.

func calendarView(_ calendarView: UICalendarView, decorationFor dateComponents: DateComponents) -> UICalendarView.Decoration? {
let font = UIFont.systemFont(ofSize: 10)
let configuration = UIImage.SymbolConfiguration(font: font)
let image = UIImage(systemName: "star.fill", withConfiguration: configuration)?.withRenderingMode(.alwaysOriginal)
return .image(image)
}

Selection in UICalendarView

For single selection

let dateSelection = UICalendarSelectionSingleDate(delegate: self)
calendarView.selectionBehavior = dateSelection

Single date selection in UICalendarView is done using UICalendarSelectionSingleDateDelegate The delegate provides us with two methods.

func dateSelection(_ selection: UICalendarSelectionSingleDate, didSelectDate dateComponents: DateComponents?) {
print("Selected Date:", dateComponents)
}


func dateSelection(_ selection: UICalendarSelectionSingleDate, canSelectDate dateComponents: DateComponents?) -> Bool {
return true
}

For multi-date selection

let dateSelection = UICalendarSelectionMultiDate(delegate: self) calendarView.selectionBehavior = dateSelection

Multi date selection in UICalendarView is done using UICalendarSelectionSingleDateDelegate The delegate provides us with two methods.

func multiDateSelection(_ selection: UICalendarSelectionMultiDate, didSelectDate dateComponents: DateComponents) {
print("Selected Date:", dateComponents)
}
func multiDateSelection(_ selection: UICalendarSelectionMultiDate, didDeselectDate dateComponents: DateComponents) {
print("De-Selected Date:", dateComponents)
}
func multiDateSelection(_ selection: UICalendarSelectionMultiDate, canSelectDate dateComponents: DateComponents) -> Bool {
return true
}
func multiDateSelection(_ selection: UICalendarSelectionMultiDate, canDeselectDate dateComponents: DateComponents) -> Bool {
return true
}

Still confession then go References

[1] https://developer.apple.com/documentation/uikit/uicalendarview

[2] https://developer.apple.com/videos/play/wwdc2022/10068/

My coding buddy

Meet Patel (iOS Developer)

Further Reading

Get notified about the details of New innovation in software industry, as well as other important development update & expansion of IT industry check our previous & Upcoming article.

Sign up to discover human stories that deepen your understanding of the world.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Shivam Thaker
Shivam Thaker

Written by Shivam Thaker

mobitech Enthusiast, who loves to write anything around mobile application Development. You can follow me here: www.linkedin.com/in/shivam-thakar-546144238

Responses (3)

Write a response

Keep writing and keep expanding the knowledge of our community

--

.After WWDC I find solution how's work with UICalender. Yor explination make so easy.

--

Thank you for the efforts you put in this amazing blog. Your guidance always supportive ❤️

--