Overloads
3 ways to call compose().toolbar(). Each is one SwiftUI overload, picked by the arguments you pass.
compose().toolbar({
content: ReactNode,
})nonisolated func toolbar<Content>(
@ContentBuilder content: () -> Content
) -> some View
where Content : ToolbarContentParameters
options.content*ReactNodeThe items representing the content of the toolbar.
Discussion
Use this method to populate a toolbar with a collection of views that you provide to a toolbar content builder.
The toolbar modifier expects a collection of toolbar items which you can
provide either by supplying a collection of views with each view
wrapped in a ToolbarItem, or by providing a collection of views as
a ToolbarItemGroup. The example below uses a collection of
ToolbarItem views to create a macOS toolbar that supports text
editing features:
struct StructToolbarItemGroupView: View {
@State private var text = ""
@State private var bold = false
@State private var italic = false
@State private var fontSize = 12.0
var displayFont: Font {
let font = Font.system(size: CGFloat(fontSize),
weight: bold == true ? .bold : .regular)
return italic == true ? font.italic() : font
}
var body: some View {
TextEditor(text: $text)
.font(displayFont)
.toolbar {
ToolbarItemGroup {
Slider(
value: $fontSize,
in: 8...120,
minimumValueLabel:
Text("A").font(.system(size: 8)),
maximumValueLabel:
Text("A").font(.system(size: 16))
) {
Text("Font Size (\(Int(fontSize)))")
}
.frame(width: 150)
Toggle(isOn: $bold) {
Image(systemName: "bold")
}
Toggle(isOn: $italic) {
Image(systemName: "italic")
}
}
}
.navigationTitle("My Note")
}
}Although it's not mandatory, wrapping a related group of toolbar
items together in a ToolbarItemGroup provides a one-to-one mapping
between controls and toolbar items which results in the correct layout
and spacing on each platform.
For design guidance on toolbars, see
doc://com.apple.documentation/design/human-interface-guidelines/toolbars
in the Human Interface Guidelines.
See also
More in Navigation.
- assistiveAccessNavigationIconConfigures the view's icon for purposes of navigation.
- asyncImageURLSessionA modifier that adds a URL session for asynchronous images contained in the view to use when fetching image data.
- contentToolbarPopulates the toolbar of the specified content view type with the views you provide.
- defaultAdaptableTabBarPlacementSpecifies the default placement for the tabs in a tab view using the adaptable sidebar style.
- defaultTabBarPlacementSpecifies the preferred placement for the tabs of a TabView in the TabViewStyle.sidebarAdaptable style on platforms where the tab bar cannot adapt between different representations, and only one representation can be shown.
- navigationBarBackButtonHiddenHides the navigation bar back button for the view.
- navigationBarTitleDisplayModeConfigures the title display mode for this view.
- navigationDestinationAssociates a destination view with a presented data type for use within a navigation stack.
- navigationDocumentConfigures the view's document for purposes of navigation.
- navigationLinkIndicatorVisibilityConfigures whether navigation links show a disclosure indicator.
- navigationSplitViewColumnWidthSets a fixed, preferred width for the column containing this view.
- navigationSubtitleConfigures the view's subtitle for purposes of navigation, using a localized string resource.