Contents

badge(_:)

Generates a badge for the view from a localized string resource.

Declaration

@export(implementation) nonisolated func badge(_ resource: LocalizedStringResource?) -> some View

Parameters

  • resource:

    An optional string resource to display as a badge. Set the value to nil to hide the badge.

Discussion

Use a badge to convey optional, supplementary information about a view. Keep the contents of the badge as short as possible. Badges appear in list rows, tab bars, toolbar items, and menus.

This modifier creates a Text view on your behalf. For more information about localizing strings, see Text. The following example shows a list with a “Default” badge on one of its rows.

NavigationView {
    List(servers) { server in
        Text(server.name)
            .badge(server.isDefault ? "Default" : nil)
    }
    .navigationTitle("Servers")
}

[Image]

See Also

Displaying a badge on a list item