ContentUnavailableView

An interface, consisting of a label and additional content, that you display when the content of your app is unavailable to users.

ComponentiOS 17.0+macOS 14.0+tvOS 17.0+watchOS 10.0+ContentUnavailableView in SwiftUI

Declaration

React
import { ContentUnavailableView } from 'swiftui-react-native';
Swift
nonisolated struct ContentUnavailableView<Label, Description, Actions> where Label : View, Description : View, Actions : View

Initializers

3 SwiftUI initializers back <ContentUnavailableView>. The props you pass pick one.

React
<ContentUnavailableView
  description?: ReactNode
  actions?: ReactNode
>
  children?: ReactNode
</ContentUnavailableView>
Swift
nonisolated init(
    @ContentBuilder label: () -> Label,
    @ContentBuilder description: () -> Description = { EmptyView() },
    @ContentBuilder actions: () -> Actions = { EmptyView() }
)
  • childrenReactNode

    Content. Strings become Text; React Native views are laid out by Yoga and placed by SwiftUI.

    Swift label: () -> Label

  • descriptionReactNode

    The view that describes the interface.

    Default `{ EmptyView() }`

  • actionsReactNode

    The content of the interface actions.

    Default `{ EmptyView() }`

Also takes the common view props: modifiers, style and testID.

Overview

It is recommended to use ContentUnavailableView in situations where a view's content cannot be displayed. That could be caused by a network error, a list without items, a search that returns no results etc.

You create an ContentUnavailableView in its simplest form, by providing a label and some additional content such as a description or a call to action:

ContentUnavailableView {
    Label("No Mail", systemImage: "tray.fill")
} description: {
    Text("New mails you receive will appear here.")
}

The system provides default ContentUnavailableViews that you can use in specific situations. The example below illustrates the usage of the ContentUnavailableView.search view:

struct ContentView: View {
    @ObservedObject private var viewModel = ContactsViewModel()

    var body: some View {
        NavigationStack {
            List {
                ForEach(viewModel.searchResults) { contact in
                    NavigationLink {
                        ContactsView(contact)
                    } label: {
                        Text(contact.name)
                    }
                }
            }
            .navigationTitle("Contacts")
            .searchable(text: $viewModel.searchText)
            .overlay {
                if searchResults.isEmpty {
                    ContentUnavailableView.search
                }
            }
        }
    }
}

From Apple’s SwiftUI documentation. Code samples there are Swift.

Relationships

Conforms to

  • View

See also

More in Text and images.