Swiftmas – Day 12 – Advent Calendar

Posted by

·

Happy Christmas Eve.! Now most advent calendar’s are over, why not build a virtual one!

For the last prompt, we have Advent Calendar. What will you create?

I’m looking forward to seeing what you come up with today. it’s Christmas Eve so I’m going to include the tutorial with the prompt…so spoiler alert…don’t scroll past this point if you don’t want the tutorial & sample project just yet!



Build an Advent Calendar App in SwiftUI

Well now that most advent calendars are now over…why not make a digital version. In just a few lines of code, we can create more fun to last as long as we’d like.

Celebrate the holiday season with an interactive Advent Calendar App! This app lets users tap on a date (from 1 to 24) to reveal a festive joke in a beautifully styled interface. Each date is displayed as a unique colored shape with random sizes for extra fun. For the last day, I have chosen not to over complicate too much as I know we’ll all be busy today. But still hopefully you get to learn or practice something new.


What You’ll Learn

  1. Using ForEach to create dynamic grids.
  2. Generating random shapes, colors, and sizes for unique designs.
  3. Displaying festive jokes with alerts.
  4. Adding polished UI elements like gradients, shadows, and text styles.

Getting Started


1. Set Up Your Project

You can find the sample project in the GitHub repository here

Create a new SwiftUI project in Xcode:

  1. Open Xcode and select Create a new project.
  2. Choose the App template, click Next.
  3. Enter your project name (e.g., “AdventCalendar”), set the interface to SwiftUI, and click Create.

Step 1: Define the Joke List

The app reveals a festive joke when a date is tapped. First, create a list of jokes stored in a constant array.

Code Snippet: Joke List

Explanation:

  • The jokes are stored in an array, where each index corresponds to a specific date (1–24).
  • When a date is tapped, the app fetches the joke at that index.

Step 2: Set Up State Variables

To manage the app’s dynamic elements (e.g., colors, shapes, and sizes), use @State properties.

Code Snippet: State Variables

Explanation:

  1. colors, shapes, sizes: Dictionaries to store random attributes for each date.
  2. isAlertVisible: Tracks whether the alert should be displayed.
  3. selectedDay: Stores the currently tapped date.

Step 3: Create the Advent Calendar Grid

Use a LazyVGrid to create a responsive grid layout displaying 24 dates. The assignedShape function will be created in just a moment.

Code Snippet: Grid Layout

Explanation:

  1. Grid Structure:
  • LazyVGrid: Displays 24 buttons in a 4-column grid.
  • Each button corresponds to a date (1–24).
  1. Dynamic Shapes and Colors:
  • Shapes, colors, and sizes are randomized for each date.
  1. Interactive Button:
  • Tapping a button calls showAlert(for:), displaying the joke for the selected date.

Step 4: Display a Joke with an Alert

When a date is tapped, show the corresponding joke using a SwiftUI Alert.

Code Snippet: Show Alert

Explanation:

  1. Track Selected Day:
  • Updates selectedDay with the tapped date.
  • Sets isAlertVisible to true to trigger the alert.
  1. Dynamic Message:
  • Fetches the joke for the selected day using selectedDay - 1 (because array indices start at 0).

Step 5: Generate Random Shapes, Colors, and Sizes

Create helper functions to assign random shapes, colors, and sizes to each date.

Code Snippet: Random Shapes

Code Snippet: Random Colors

Code Snippet: Random Sizes

Explanation:

  • Shapes (Circle, RoundedRectangle, Capsule) are randomly selected.
  • Colors and sizes are generated once per date and cached in their respective dictionaries.

Step 6: Add a Festive Background

Enhance the app’s appearance with a radial gradient background.

Code Snippet: Background Gradient

Explanation:

  • Creates a festive gradient radiating from the center.
  • Uses holiday-inspired colors like red, green, and yellow.

Complete Code

Here’s the final code for your Advent Calendar App:


Step 7: Run Your App

Run the app on an iOS Simulator or device. Test the following features:

  • Each shape is randomly set & colored.
  • Alert shows on each click of a door.
  • You get a good laugh out of each joke.

What’s Next?

  • Mark Completed Days: Add a visual indicator for days the user has already opened.
  • Daily Rewards: Include festive images or messages behind each date.
  • Animations: Add bounce or fade effects when tapping a date.
  • Sounds: Festive sounds when a door is unlocked

🎉 Congratulations! You’ve built a fun and festive Advent Calendar App and with that, we come to the end of this year’s Swiftmas Challenge! Thank you so much for joining in each day. I hope you have enjoyed the challenge & trying new things. Have a very Merry Christmas and here’s to a brilliant 2025 ahead! 🎄


Discover more from

Subscribe to get the latest posts sent to your email.

thecodingsprite avatar

About the author

Hi! My name is Billie, my friends call me Billie Boo. I am a self taught iOS developer with a background in computer science, animation, graphic design & web design. I love sharing my knowledge & projects with the world & that is my mission for this blog. It’s never too late or too hard to follow your dreams.