Skip to content
DeveloperMemos

Taking a Look at SwiftUI's ToolbarItem

SwiftUI, ToolbarItem1 min read

SwiftUI provides a wide range of powerful and flexible tools for building user interfaces. One such tool is the ToolbarItem, which allows you to add various items to your app's toolbar. In this article, we'll take a closer look at SwiftUI's ToolbarItem and explore its capabilities with some examples.

Introduction to ToolbarItem

The ToolbarItem is a view that represents an item in the toolbar of your SwiftUI app. It can be used to add buttons, menus, or other interactive elements to the toolbar. The ToolbarItem can be placed in different positions within the toolbar, such as leading, trailing, or center.

To add a ToolbarItem to your app's toolbar, you need to use the .toolbar modifier on the NavigationView or Toolbar view. Within the .toolbar modifier, you can add one or more ToolbarItem instances, specifying their position and content.

Let's dive into some examples to see how ToolbarItem can be used effectively in SwiftUI.

Example 1: Adding a Button to the Toolbar

1struct ContentView: View {
2 var body: some View {
3 NavigationView {
4 Text("Hello, World!")
5 .toolbar {
6 ToolbarItem(placement: .navigationBarTrailing) {
7 Button(action: {
8 // Action to be performed when the button is tapped
9 }) {
10 Image(systemName: "plus")
11 }
12 }
13 }
14 }
15 }
16}

In this example, we have a ToolbarItem with a placement of .navigationBarTrailing, which adds a button with a plus icon to the trailing side of the navigation bar. You can customize the appearance and behavior of the button by providing a closure for the action parameter.

Example 2: Adding a Menu to the Toolbar

1struct ContentView: View {
2 var body: some View {
3 NavigationView {
4 Text("Hello, World!")
5 .toolbar {
6 ToolbarItem(placement: .navigationBarTrailing) {
7 Menu {
8 Button(action: {
9 // Action for the first menu item
10 }) {
11 Label("Option 1", systemImage: "square.fill")
12 }
13 Button(action: {
14 // Action for the second menu item
15 }) {
16 Label("Option 2", systemImage: "circle.fill")
17 }
18 } label: {
19 Image(systemName: "ellipsis.circle")
20 }
21 }
22 }
23 }
24 }
25}

In this example, we use a ToolbarItem with a placement of .navigationBarTrailing to add a menu to the trailing side of the navigation bar. The menu contains two menu items represented by buttons. You can customize the appearance and actions of the menu items according to your app's requirements.

Example 3: Adding Multiple Toolbar Items

1struct ContentView: View {
2 var body: some View {
3 NavigationView {
4 Text("Hello, World!")
5 .toolbar {
6 ToolbarItem(placement: .navigationBarLeading) {
7 Button(action: {
8 // Action for the leading button
9 }) {
10 Image(systemName: "arrow.backward")
11 }
12 }
13 ToolbarItem(placement: .navigationBarTrailing) {
14 Button(action: {
15 //
16
17 Action for the trailing button
18 }) {
19 Image(systemName: "gear")
20 }
21 }
22 }
23 }
24 }
25}

In this example, we showcase how to add multiple ToolbarItem instances to the navigation bar. One ToolbarItem with a placement of .navigationBarLeading adds a back button to the leading side of the navigation bar, while another ToolbarItem with a placement of .navigationBarTrailing adds a settings button to the trailing side.

Wrap Up

SwiftUI's ToolbarItem provides a convenient way to add buttons, menus, and other interactive elements to your app's toolbar. With its flexible placement options, you can customize the toolbar to suit your app's specific needs. We explored some basic examples to demonstrate the usage of ToolbarItem, but there are many more possibilities to discover. So go ahead, experiment with ToolbarItem, and enhance the usability of your SwiftUI apps.