FREE TRIAL: Accelerate your app development career with Hacking with Swift+! >>

How to add bar items to a navigation view

Paul Hudson    @twostraws   

Updated for Xcode 13.0 beta 2

The toolbar() modifier lets us add single or multiple bar button items to the leading and trailing edge of a navigation view, 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 a single Help button the to trailing edge of a navigation view:

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

Download this as an Xcode project

We haven’t specified where the button should be displayed, but that’s okay – SwiftUI knows 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 {
        NavigationView {
            Text("SwiftUI")
                .navigationTitle("Welcome")
                .toolbar {
                    ToolbarItem(placement: .navigationBarLeading) {
                        Button("Help") {
                            print("Help tapped!")
                        }
                    }
                }
        }
    }
}

Download this as an Xcode project

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 {
        NavigationView {
            Text("SwiftUI")
                .navigationTitle("Welcome")
                .toolbar {
                    ToolbarItemGroup(placement: .navigationBarTrailing) {
                        Button("About") {
                            print("About tapped!")
                        }

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

Download this as an Xcode project

Hacking with Swift is sponsored by Essential Developer

SPONSORED From August 2nd to 8th you can join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer!

Save your spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Similar solutions…

BUY OUR BOOKS
Buy Pro Swift Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift (Vapor Edition) Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Server-Side Swift (Kitura Edition) Buy Beyond Code

Was this page useful? Let us know!

Average rating: 4.1/5

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.