How to Use SwiftUI to Create a Chess Board on iOS

Creating a chess board in an iOS app might sound intimidating, but SwiftUI makes the process much more approachable. Even if you’re new to development, you can build the foundation of a chess app without diving deep into complex code. In this guide, we’ll walk through the concepts behind setting up a chess board in SwiftUI, how the layout works, and what steps you can take to expand your project later. Think of it as a beginner-friendly roadmap, not just for chess, but for learning SwiftUI itself.

Why SwiftUI is Perfect for a Chess App

SwiftUI is Apple’s modern framework for building user interfaces. Unlike older tools, SwiftUI is:

  • Declarative: You describe what the interface should look like, and SwiftUI handles the details.

  • Visual: You can preview your design in real time while you code.

  • Consistent: It works across iPhone, iPad, Mac, and even Apple Watch.


For something like a chess board, where the layout is predictable and symmetrical, SwiftUI is an ideal choice. You’ll be working with grids, colors, and images—all areas where SwiftUI shines.

Breaking Down the Chess Board

Before we jump into SwiftUI concepts, let’s think about what a chess board really is:

  • It has 8 rows and 8 columns.

  • Each square is either light or dark, arranged in a checkered pattern.

  • The board itself is just a repeating grid of squares.


This structure maps perfectly to SwiftUI’s way of handling layouts. By imagining the board as a grid of views (tiny reusable pieces of UI), you can build it step by step instead of treating it as one giant block.

Step 1: Understanding the Grid Concept

In SwiftUI, layouts often use stacks:

  • HStack (horizontal stack) places items side by side.

  • VStack (vertical stack) places items on top of each other.

  • LazyVGrid or LazyHGrid is great for grids, like a chess board.


A chess board is simply a grid of 64 squares. Each square is just a small view with a background color. So, the very first step is to think in terms of repeating rows and columns.

Step 2: Making the Squares

Each square on the chess board is a “cell.” You only need to define one type of cell, and SwiftUI lets you reuse it across the entire board. A square can have two simple states: light or dark.

In practice, you’d create a reusable Square view that changes color depending on whether it should be light or dark. Once you have that, filling the grid becomes automatic.

Step 3: Alternating Colors

The signature look of a chess board comes from the alternating pattern. One row starts with a light square, the next starts with a dark square, and so on.

This is where simple logic comes in:

  • If the sum of the row and column index is even, make the square light.

  • If it’s odd, make the square dark.


You don’t need to understand advanced math to see the pattern—just picture the way tiles alternate on a floor. SwiftUI can calculate this pattern for you behind the scenes once you set the rule.

Step 4: Adding Pieces

Once the board is in place, the next step is adding chess pieces. Pieces are just images or symbols placed on top of the squares. You can start with simple icons (like “♔” for the king or “♟︎” for the pawn) or use image files of actual chess pieces.

In SwiftUI, each square can hold not just a background color, but also content. That means you can “stack” a piece on top of its square. For example:

  • A dark square with a white pawn image.

  • A light square with no piece at all.


This layering is done with ZStack, which allows you to put items on top of each other.

Step 5: Responding to Touch

What makes an app more than just a static picture is interaction. SwiftUI makes it simple to respond to taps or gestures. For a chess board, that means:

  • Tapping a piece to select it.

  • Highlighting available moves.

  • Dragging a piece from one square to another.


Even if you don’t build the full game logic right away, you can practice by making the squares change color when tapped. That small step helps you learn how state and interaction work in SwiftUI.

Step 6: Keeping Track of the Game

Behind the visuals, a chess board needs structure. SwiftUI uses state variables to keep track of changes. For example, you could have:

  • A list that represents the position of each piece.

  • A variable that remembers which piece is currently selected.

  • Rules that prevent illegal moves.


You don’t need to implement the full complexity of chess on day one. Start with basics like “move a piece from square A to square B” and expand as you go.

Step 7: Expanding Your Project

Once you have the basics, you can add features step by step:

  • Timers for competitive games.

  • Move history that lists all past moves.

  • AI opponent using simple logic or external libraries.

  • Online play by connecting your app to a server.


The beauty of SwiftUI is that you can keep layering features without rebuilding from scratch.

Common Pitfalls for Beginners

When building your first chess board, here are a few challenges you might face:

  1. Getting the layout wrong: It’s easy to miscount rows or columns. Always double-check that you have 64 squares.

  2. Scaling the board: Make sure your squares resize correctly on different screen sizes. SwiftUI handles this, but test on iPhone and iPad.

  3. Overcomplicating too early: Don’t start with AI or multiplayer. Get the board and simple movement working first.

Why Building a Chess Board is a Great Learning Project

You might wonder why chess is often used as an example for programming. The reason is simple:

  • It combines visual layout (the board) with logic (the rules).

  • It forces you to think about state management—how things change over time.

  • It can grow with you, from a simple static board to a fully functional game.


If you can build a chess board in SwiftUI, you’ll have learned skills that apply to almost any other app: grids, user interaction, state, and design thinking.

Final Thoughts

Creating a chess board with SwiftUI isn’t just about building a game. It’s a hands-on way to learn how modern iOS development works. You’ll practice thinking in grids, alternating colors, handling user interaction, and managing state—all core skills for any app.

Start small: create the 8×8 grid. Then, add alternating colors. Place pieces. Make them respond to taps. Over time, you’ll find that you’ve not only built a chess board but also developed the confidence to tackle bigger projects in SwiftUI.

You may also like...

Popular Posts