How do I create a toolbar in swiftUI
Asked on 2024-07-30
1 search
To create a toolbar in SwiftUI, you can use the toolbar modifier on a view. This allows you to add toolbar items and groups to your app's interface. Here's a basic example of how to create a toolbar in SwiftUI:
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationView {
Text("Hello, World!")
.toolbar {
ToolbarItem(placement: .navigationBarLeading) {
Button(action: {
// Action for the button
}) {
Image(systemName: "plus")
}
}
ToolbarItem(placement: .navigationBarTrailing) {
Button(action: {
// Action for the button
}) {
Image(systemName: "trash")
}
}
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
In this example, the toolbar modifier is used to add two buttons to the navigation bar, one on the leading side and one on the trailing side.
For more advanced usage, such as adding toolbars to volumes in immersive spaces, you can refer to the session Dive deep into volumes and immersive spaces. This session covers how to add toolbars to volumes, including specifying ornament placements and adding buttons to perform different actions.
Relevant Sessions

What’s new in AppKit
Discover the latest advances in Mac app development. Get an overview of the new features in macOS Sequoia, and how to adopt them in your app. Explore new ways to integrate your existing code with SwiftUI. Learn about the improvements made to numerous AppKit controls, like toolbars, menus, text input, and more.

SwiftUI essentials
Join us on a tour of SwiftUI, Apple’s declarative user interface framework. Learn essential concepts for building apps in SwiftUI, like views, state variables, and layout. Discover the breadth of APIs for building fully featured experiences and crafting unique custom components. Whether you’re brand new to SwiftUI or an experienced developer, you’ll learn how to take advantage of what SwiftUI has to offer when building great apps.

Tailor macOS windows with SwiftUI
Make your windows feel tailor-made for macOS. Fine-tune your app’s windows for focused purposes, ease of use, and to express functionality. Use SwiftUI to style window toolbars and backgrounds. Arrange your windows with precision, and make smart decisions about restoration and minimization.
