Skip to main content

How to tell the user that no content is available

About 2 minSwiftSwiftUIArticle(s)bloghackingwithswift.comcrashcourseswiftswiftuixcodeappstore

How to tell the user that no content is available 관련

SwiftUI by Example

Back to Home

How to tell the user that no content is available | SwiftUI by Example

How to tell the user that no content is available

Updated for Xcode 15

New in iOS 17

SwiftUI has a dedicated ContentUnavailableView type designed to show users when nothing is available to see. For example, if they perform a search that yields no results, using ContentUnavailableView is much nicer than showing a blank screen.

In its simplest form, you just need to use this code to show a failed search results screen:

struct ContentView: View {
    var body: some View {
        ContentUnavailableView.search
    }
}

Download this as an Xcode projectopen in new window

The default ContentUnavailableView, showing No Results and a prompt for the user to try again.
The default ContentUnavailableView, showing No Results and a prompt for the user to try again.

You’ll see a magnifying glass icon, backed up by title and subtitle text explaining that the user’s search yielded no results.

You can customize it if you want, to add what the user was searching for:

ContentUnavailableView.search(text: "Life, the Universe, and Everything")

Download this as an Xcode projectopen in new window

A slightly customized ContentUnavailableView, showing the user’s search string and asking them to try again.
A slightly customized ContentUnavailableView, showing the user’s search string and asking them to try again.

But you can also customize the icon and description too, giving a completely bespoke result:

ContentUnavailableView("No favorites", systemImage: "star", description: Text("You don't have any favorites yet."))
    .symbolVariant(.slash)

Download this as an Xcode projectopen in new window

A customize ContentUnavailableView, showing No Favorites and a message telling the user they have no favorites.
A customize ContentUnavailableView, showing No Favorites and a message telling the user they have no favorites.
Similar solutions…
How to create multi-column lists using Table | SwiftUI by Example

How to create multi-column lists using Table
How to mark content as private using privacySensitive() | SwiftUI by Example

How to mark content as private using privacySensitive()
How to hide and reveal content using DisclosureGroup | SwiftUI by Example

How to hide and reveal content using DisclosureGroup
How to indent the content or scroll indicators in a ScrollView | SwiftUI by Example

How to indent the content or scroll indicators in a ScrollView
How to place content outside the safe area | SwiftUI by Example

How to place content outside the safe area
이찬희 (MarkiiimarK)
Never Stop Learning.