How it works
ToolbarTitleMenu is a toolbar item that attaches a pull-down menu directly to a navigation bar's title, surfacing actions that relate to the current view as a whole rather than to a single control. It gives people a familiar place to switch contexts or invoke document-level commands by tapping the title itself, which the system marks with a chevron to signal that it's interactive. Reach for it when the title names something the user can act on or navigate between — a document, a folder, an account — and you want those commands one tap away without crowding the toolbar with extra buttons.
Place it inside a toolbar { } builder
ToolbarTitleMenuis a toolbar content type, so it only takes effect when returned from a.toolbarmodifier. In the example, theToolbarTitleMenu { ... }lives in the.toolbarattached to theText(folder)view, which lets SwiftUI bind the menu to that screen's navigation bar.Pair it with a navigation title
The menu hangs off the title established by
.navigationTitle, so the symbol needs a title to attach to. Here.navigationTitle(folder)supplies the text the chevron decorates, and because both the title and the menu read the samefolderstate, the displayed title is also the thing the menu controls.Provide the menu items as a content closure
ToolbarTitleMenutakes a@ViewBuilderclosure describing the menu's contents — typically a set ofButtons or other menu-friendly views. In the example the closure holds threeButtons ("Inbox","Sent","Archive"), each becoming a row in the pull-down.Drive state from the actions
Because the items are ordinary views, their actions mutate app state like any other control. Each
Buttonhere assigns a new value tofolder(for instancefolder = "Sent"), and since the title is bound to@State private var folder, picking an item updates both the title and the body text at once.
Button("Spam") { folder = "Spam" } inside the ToolbarTitleMenu closure, then tap the navigation title to see the new option appear in the menu and rename the screen.Example & preview
Press Run live & edit to compile it in your browser — then edit the Swift on the left and the preview re-renders live.
struct ToolbarTitleMenuDemo: View {
@State private var folder = "Inbox"
var body: some View {
NavigationStack {
Text(folder)
.font(.title2)
.padding()
.navigationTitle(folder)
.toolbar {
ToolbarTitleMenu {
Button("Inbox") { folder = "Inbox" }
Button("Sent") { folder = "Sent" }
Button("Archive") { folder = "Archive" }
}
}
}
}
}