Creating a SwiftUI List with children and an expandable second layer is an essential skill for iOS developers who want to build user-friendly interfaces. Whether you’re working on a hierarchical menu, file explorer, or category structure, using a parent-child relationship with expandable layers can greatly enhance user navigation. In this detailed guide, we’ll cover everything you need to know, step by step, with easy-to-understand examples.
What is a SwiftUI List with Children?
A SwiftUI List with children refers to a structured view that displays a list of items, where some of the items act as “parent nodes,” and others act as “child nodes.” A parent node contains a group of related child items that can either be shown or hidden depending on whether the parent is expanded or collapsed. This hierarchical representation makes navigation intuitive and keeps the user interface clean.
For example, consider an app where you want to display categories and subcategories, such as:
- Fruits
- Apples
- Oranges
- Bananas
- Vegetables
- Carrots
- Broccoli
Here, “Fruits” and “Vegetables” are parent nodes, while their respective sub-items are child nodes. SwiftUI makes it simple to create such nested lists using a combination of List, ForEach, and state management techniques.
Why Add an Expandable Second Layer?
Adding an expandable second layer to your SwiftUI List is crucial for improving user experience and efficiently organizing large sets of data. Here are the key benefits:
- Improved Usability: When users can expand or collapse sections as needed, they don’t feel overwhelmed by too much information displayed all at once. This improves clarity and focus.
- Enhanced Navigation: Expandable layers allow users to drill down into specific categories or groups, enabling a more organized way to explore content.
- Performance Optimization: By only loading or displaying child items when a parent node is expanded, you can save system resources and keep your app running smoothly, especially with large datasets.
- Customizable Experience: Expandable layers allow you to give users more control over what they see, making your app more dynamic and responsive to their needs.
Step-by-Step Guide to Build Your List
Here’s how to create a SwiftUI List with children and an expandable second layer. We’ll break it down into smaller steps for clarity.

Create a Simple List in SwiftUI
To start, you need to create a basic List in SwiftUI. The List view is used to display rows of data in a scrollable, vertical format. Here’s a simple example:
expandedItems
keeps track of which parent nodes are currently expanded.
- The toggleExpansion() function adds or removes an item’s ID from the expandedItems set when the user taps the expand/collapse button.
Coding Example for Nested Lists in SwiftUI
Let’s combine everything into a single, functional example:
Tips to Make Your SwiftUI List Work Smoothly
List Doesn’t Expand

- Ensure that your parent-child data structure is set up correctly. Check for missing or incorrectly initialized children.
Data Not Displayed Correctly
- Debug the expandedItems set to confirm that IDs are being tracked properly.
- Test with various datasets to ensure the app adapts dynamically.
Performance Issues with Large Lists
- Consider lazy loading for large datasets by implementing LazyVStack with ForEach.
- Use on-demand loading techniques to fetch child nodes only when a parent is expanded.
Add Animations for a Better User Experience
Adding animations can make your expandable list more engaging. Use the .animation() modifier to smoothly expand or collapse sections:
swift
CopyEdit
withAnimation {
toggleExpansion()
}
This ensures transitions between states are fluid and visually appealing.
The Bottom Line
Creating a SwiftUI List with children and expandable second layers is straightforward when you understand the basics of state management, data modeling, and SwiftUI’s powerful tools. By following the steps and examples outlined above, you’ll be able to build dynamic, user-friendly interfaces that enhance your app’s functionality and user experience.