Contents

navigationBarItems(leading:trailing:)

Sets the navigation bar items for this view.

Declaration

nonisolated func navigationBarItems<L, T>(leading: L, trailing: T) -> some View where L : View, T : View

Parameters

  • leading:

    A view that appears on the leading edge of the title.

  • trailing:

    A view that appears on the trailing edge of the title.

Discussion

Use navigationBarItems(leading:trailing:) to add navigation bar items to the leading and trailing edges of the navigation bar for this view.

This modifier only takes effect when this view is inside of and visible within a NavigationView.

On iOS 14 and later, the leading item supplements a visible back button, instead of replacing it, by default. To hide the back button, use navigationBarBackButtonHidden(_:).

The example below adds buttons to the leading and trailing edges of the button area of the navigation view:

struct FlavorView: View {
    var body: some View {
        NavigationView {
            List {
                Text("Chocolate")
                Text("Vanilla")
                Text("Strawberry")
            }
            .navigationBarTitle(Text("Today‘s Flavors"))
            .navigationBarItems(leading:
                HStack {
                    Button("Hours") {
                        print("Hours tapped!")
                    }
                }, trailing:
                HStack {
                    Button("Favorites") {
                        print("Favorites tapped!")
                    }

                    Button("Specials") {
                        print("Specials tapped!")
                    }
                }
            )
        }
    }
}

See Also

Auxiliary view modifiers