Skip to main content

How to add bar items to a navigation view

About 3 minSwiftSwiftUIArticle(s)bloghackingwithswift.comcrashcourseswiftswiftuixcodeappstore

How to add bar items to a navigation view 관련

SwiftUI by Example

Back to Home

How to add bar items to a navigation view | SwiftUI by Example

How to add bar items to a navigation view

Updated for Xcode 15

The toolbar() modifier lets us add single or multiple bar button items to the leading and trailing edge of a navigation stack, as well as other parts of our view if needed. These might be tappable buttons, but there are no restrictions – you can add any sort of view.

For example, this adds two buttons to the trailing edge of a navigation bar:

struct ContentView: View {
    var body: some View {
        NavigationStack {
            Text("SwiftUI")
                .navigationTitle("Welcome")
                .toolbar {
                    Button("About") {
                        print("About tapped!")
                    }

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

Download this as an Xcode projectopen in new window

We haven't specified where the buttons should be displayed, but that's okay – SwiftUI knows that on iOS the trailing edge is the best place for left to right languages, and will automatically flip that to the other side for right to left languages.

If you want to control the exact position of your button, you can do so by wrapping it in a ToolbarItem and specifying the placement you want. For example, this will create a button and force it be on the leading edge of the navigation bar:

struct ContentView: View {
    var body: some View {
        NavigationStack {
            Text("SwiftUI")
                .navigationTitle("Welcome")
                .toolbar {
                    ToolbarItem(placement: .navigationBarLeading) {
                        Button("Help") {
                            print("Help tapped!")
                        }
                    }
                }
        }
    }
}

Download this as an Xcode projectopen in new window

If you want to place multiple bar buttons in different locations, you can just repeat ToolbarItem as many times as you need and specify a different placement each time.

To put multiple bar buttons in the same locations, you should wrap them in a ToolbarItemGroup, like this:

struct ContentView: View {
    var body: some View {
        NavigationStack {
            Text("SwiftUI")
                .navigationTitle("Welcome")
                .toolbar {
                    ToolbarItemGroup(placement: .primaryAction) {
                        Button("About") {
                            print("About tapped!")
                        }

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

Download this as an Xcode projectopen in new window

That uses the .primaryAction placement, which will position the buttons depending on where the platform thinks the most important buttons go.

There's also a .secondaryAction placement designed for actions that are useful but not required, and on iOS that will cause buttons in that group to be collapsed down to a single details button.

So, in this code sample some buttons are always displayed, and others are hidden under a menu:

NavigationStack {
    Text("SwiftUI")
        .navigationTitle("Welcome")
        .toolbar {
            ToolbarItemGroup(placement: .primaryAction) {
                Button("About") {
                    print("About tapped!")
                }

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

            ToolbarItemGroup(placement: .secondaryAction) {
                Button("Settings") {
                    print("Credits tapped")
                }

                Button("Email Me") {
                    print("Email tapped")
                }
            }
        }
}

Download this as an Xcode projectopen in new window

Similar solutions…
How to hide the tab bar, navigation bar, or other toolbars | SwiftUI by Example

How to hide the tab bar, navigation bar, or other toolbars
SwiftUI tips and tricks | SwiftUI by Example

SwiftUI tips and tricks
How to add a search bar to filter your data | SwiftUI by Example

How to add a search bar to filter your data
Adding items to an order with @EnvironmentObject | SwiftUI by Example

Adding items to an order with @EnvironmentObject
How to embed a view in a navigation view | SwiftUI by Example

How to embed a view in a navigation view

이찬희 (MarkiiimarK)
Never Stop Learning.