
- SwiftUI - Home
- SwiftUI - Overview
- SwiftUI vs UIkit
- SwiftUI Environment
- SwiftUI - Environment Setup
- SwiftUI - Basic Components
- SwiftUI - Building First Application
- SwiftUI Views
- SwiftUI - Views
- SwiftUI - Customize Text View
- SwiftUI - Custom Image View
- SwiftUI - Stacks
- SwiftUI Drawing Shapes
- SwiftUI - Shapes
- SwiftUI - Drawing line
- SwiftUI - Drawing Rectangle
- SwiftUI - Drawing Rounded Rectangle
- SwiftUI - Drawing Triangle
- SwiftUI - Drawing Circle
- SwiftUI - Drawing Star
- SwiftUI - Drawing Polygon
- SwiftUI - Drawing Pie chart
- SwiftUI - Using built-in shapes
- SwiftUI - Text
- SwiftUI - Text View
- SwiftUI - Text Input and Output
- SwiftUI - Color
- SwiftUI - Color
- SwiftUI - Colorpicker
- SwiftUI - Gradients
- SwiftUI - Adjust Color
- SwiftUI - Effects
- SwiftUI - Effects
- SwiftUI - Blend Effect
- SwiftUI - BLur Effect
- SwiftUI - Shadow Effect
- SwiftUI - Hover Effect
- SwiftUI - Animations
- SwiftUI - Animations
- SwiftUI - Creating Animations
- SwiftUI - Creating an Explicit Animation
- SwiftUI - Multiple Animations
- SwiftUI - Transitions
- SwiftUI - Asymmetric Transition
- SwiftUI - Custom Transition
- SwiftUI - Image
- SwiftUI - Images
- SwiftUI - Image as Background
- SwiftUI - Rotating Image
- SwiftUI - Media
- SwiftUI - View Layout
- SwiftUI - View Layout
- SwiftUI - View Size
- SwiftUI - View Spacing
- SwiftUI - View Padding
- SwiftUI - UI Controls
- SwiftUI - UI Controls
- SwiftUI - Button
- SwiftUI - CheckBox
- SwiftUI - Menubar
- SwiftUI - Toolbar
- SwiftUI - Search Bar
- SwiftUI - TextField
- SwiftUI - Slider
- SwiftUI - Toggle
- SwiftUI - Pickers
- SwiftUI - Menus
- SwiftUI - List & Tables
- SwiftUI - Lists
- SwiftUI - Static List
- SwiftUI - Dynamic List
- SwiftUI - Customize List
- SwiftUI - Tables
- SwiftUI - Forms
- SwiftUI - Forms
- SwiftUI - Breaking Forms in Sections
- SwiftUI - Event Handling
- SwiftUI - Event Handling
- SwiftUI - Gesture
- SwiftUI - Clipboard
- SwiftUI - Drag and Drop
- SwiftUI - Focus
- SwiftUI - Alert
- SwiftUI - Miscellaneous
- SwiftUI - Containers
- SwiftUI - Navigation
- SwiftUI - Notifications
- SwiftUI - Cross-Platform UI
- SwiftUI - Data
- SwiftUI - Accessibility
- SwiftUI - Framework Integration
- SwiftUI - Framework Integration
- SwiftUI - Interfacing with UIKit
- SwiftUI - Creating macOS App
- SwiftUI Useful Resources
- SwiftUI - Useful Resources
- SwiftUI - Discussion
SwiftUI - ToolBar
Toolbar is a UI control in SwiftUI which is used to create a bar at the top or bottom of the app that contains buttons, titles, menus, or other items. It provides a most convenient way to display multiple controls together in a bar on the current view.
Generally, a toolbar is used with navigation views such as NavigationView or NavigationStack to enhance the user experience. Toolbar UI control generally works well with iOS, macOS, and watchOS.
Creating ToolBar in SwiftUI
In SwiftUI, we can create a toolbar with the help of toolbar() method. This method creates a toolbar with the given elements, where we can add elements in the toolbar with the help of ToolbarItem or ToolbarItemGroup.
ToolbarItem adds one element at a time in the toolbar, whereas ToolbarItemGroup adds multiple elements at the same time in the specified toolbar. A toolbar can contain various UI components such as Buttons, Sliders, Toggles, etc.
Syntax
Following is the syntax −
Toolbar{ ToolbarItem(placement: ToolbarItemPlacement){ //code } ToolbarItemGroup{ //code } }
We can also adjust the placement of the elements in the toolbar with the help of the placement parameter. It provides various type of placements such as .automatic, .topBarLeading, toBarTrailing, .buttonBar, etc.
Example 1
The following SwiftUI program creates a toolbar with one button.
import SwiftUI struct ContentView: View { var body: some View { NavigationView { Text("SwiftUI") .navigationTitle("Toolbar") // Toolbar with a single button .toolbar { ToolbarItem(placement: .navigationBarTrailing) { Button(action: { print("Button tapped!") }) { Image(systemName: "plus").font(.largeTitle) }.background(.blue).foregroundStyle(.white) } } } } } #Preview { ContentView() }
Output

Example 2
The following SwiftUI program creates a toolbar which contains multiple elements such as system images, buttons, toggle, and slider.
import SwiftUI struct ContentView: View { @State private var value1: Double = 5 @State private var value2: Bool = true var body: some View { NavigationView { Text("TutorialsPoint") .font(.title) .toolbar { // Leading item ToolbarItem(placement: .navigationBarLeading) { Button(action: { print("Leading button tapped!") }) { Image(systemName: "chevron.left") } } // Trailing item 1 - Slider ToolbarItem(placement: .navigationBarTrailing) { Slider(value: $value1, in: 0...10) .frame(width: 150) } // Trailing item 2 - Toggle ToolbarItem(placement: .navigationBarTrailing) { Toggle("On/Off", isOn: $value2) .padding() } // Trailing item 3 - Button ToolbarItem(placement: .navigationBarTrailing) { Button(action: { print("Trailing button tapped!") }) { Image(systemName: "gear") } } } } } } #Preview { ContentView() }
Output
