ContentUnavailableView
An interface, consisting of a label and additional content, that you display when the content of your app is unavailable to users.
Declaration
Initializers
3 SwiftUI initializers back <ContentUnavailableView>. The props you pass pick one.
<ContentUnavailableView
description?: ReactNode
actions?: ReactNode
>
children?: ReactNode
</ContentUnavailableView>nonisolated init(
@ContentBuilder label: () -> Label,
@ContentBuilder description: () -> Description = { EmptyView() },
@ContentBuilder actions: () -> Actions = { EmptyView() }
)childrenReactNodeContent. Strings become
Text; React Native views are laid out by Yoga and placed by SwiftUI.Swift
label: () -> LabeldescriptionReactNodeThe view that describes the interface.
Default
`{ EmptyView() }`actionsReactNodeThe 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.
- AsyncImageA view that asynchronously loads and displays an image.
- ImageA view that displays an image.
- LabelA standard label for user interface items, consisting of an icon with a title.
- LabeledContentA container for attaching a label to a value-bearing view.
- ProgressViewA view that shows the progress toward completion of a task.
- TextA view that displays one or more lines of read-only text.