Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • SwiftUI Cookbook
  • Toc
  • feedback
SwiftUI Cookbook

SwiftUI Cookbook

By : Giordano Scalzo, Nzokwe
4.3 (20)
close
SwiftUI Cookbook

SwiftUI Cookbook

4.3 (20)
By: Giordano Scalzo, Nzokwe

Overview of this book

SwiftUI provides an innovative and simple way to build beautiful user interfaces (UIs) for all Apple platforms, from iOS and macOS through to watchOS and tvOS, using the Swift programming language. In this recipe-based cookbook, you’ll cover the foundations of SwiftUI as well as the new SwiftUI 3 features introduced in iOS 15 and explore a range of essential techniques and concepts that will help you through the development process. The cookbook begins by explaining how to use basic SwiftUI components. Once you’ve learned the core concepts of UI development, such as Views, Controls, Lists, and ScrollViews, using practical implementations in Swift, you'll advance to adding useful features to SwiftUI using drawings, built-in shapes, animations, and transitions. You’ll understand how to integrate SwiftUI with exciting new components in the Apple development ecosystem, such as Combine for managing events and Core Data for managing app data. Finally, you’ll write iOS, macOS, and watchOS apps by sharing the same SwiftUI codebase. By the end of this SwiftUI book, you'll have discovered a range of simple, direct solutions to common problems encountered when building SwiftUI apps.
Table of Contents (17 chapters)
close

Adding sections to a list

In this recipe, we will create an app that implements a static list with sections. The app will display a list of countries grouped by continent.

Getting ready

Let's start by creating a new SwiftUI app in Xcode named ListWithSections.

How to do it…

We will add a Section view to our List to separate groups of items by section titles. Proceed as follows:

  1. (Optional) Open the ContentView.swift file and replace the Text view with a NavigationView. Wrapping the List in a NavigationView allows us to add a title and navigation items to the view:
    NavigationView{
    }
  2. Add a list and section to NavigationView (or body view if you skipped optional Step 1). Also, add a listStyle and navigationBarTitle modifier:
    List {
        Section(header: Text("North America")){
            Text("USA")
            Text("Canada")
            Text("Mexico")
            Text("Panama")
            Text("Anguilla")
        }
    }
    .listStyle(.grouped)
    .navigationBarTitle("Continents and Countries",
      displayMode: .inline)
  3. Below the initial Section, add more sections representing countries in various continents:
                List {
                    …
                    Section(header: Text("Africa")){
                        Text("Nigeria")
                        Text("Ghana")
                        Text("Kenya")
                        Text("Senegal")
                    }
                    Section(header: Text("Europe")){
                        Text("Spain")
                        Text("France")
                        Text("Sweden")
                        Text("Finland")
                        Text("UK")
                    }
                }

    Your canvas preview should resemble the following:

Figure 2.9 – ListWithSections preview

Figure 2.9 – ListWithSections preview

Looking at the preview, you can see the continents where each country is located by reading the section titles.

How it works…

SwiftUI's Section views are used to separate items into groups. In this recipe, we used Section views to visually group countries by their continents. A Section view can be used with a header, as shown in this recipe, or without a header, as follows:

                Section {
                    Text("Spain")
                    Text("France")
                    Text("Sweden")
                    Text("Finland")
                    Text("UK")
                }

You can change section styles by using the listStyle() modifier with the .grouped style.

bookmark search playlist download font-size

Change the font size

margin-width

Change margin width

day-mode

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Delete Bookmark

Modal Close icon
Are you sure you want to delete it?
Cancel
Yes, Delete