badge

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

View ModifieriOS 16.0+macOS 13.0+

Overloads

2 ways to call compose().badge(). Each is one SwiftUI overload, picked by the arguments you pass.

React
compose().badge(resource: string | null)
Swift
@export(implementation) nonisolated func badge(
    _ resource: LocalizedStringResource?
) -> some View
3 more Swift overloads share this form
Swift
nonisolated func badge(_ key: LocalizedStringKey?) -> some View
Swift
nonisolated func badge(_ label: Text?) -> some View
Swift
nonisolated func badge<S>(
    _ label: S?
) -> some View
where S : StringProtocol

Parameters

  • resource*string | null

    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")
}

See also

More in Appearance.