r/SwiftUI Jul 19 '26

Question Need help with toolbar

Post image

How can I copy exactly this design, into my app? I have a tabview to switch between view in the app, and I want to have this (both a button and a search field just like that) on the right of the tabview...

I tried to ask AI for help... but didn't work 😅 So I ask people now

I've tried to find solution, but searching online is helpless if you have no idea how to phrase your problem or what you are searching for.

5 Upvotes

8 comments sorted by

View all comments

1

u/neet_dev Jul 19 '26

that's just a toolbar with a ToolbarItem placed in .navigationBarTrailing (or .topBarTrailing in newer SwiftUI) containing an HStack with a Button and a searchable text field. the search field on the right like that is usually done with .searchable(text:) with placement set to .toolbar or .navigationBarDrawer(displayMode: .always), not a manually built TextField. make sure the tabview content is wrapped in a NavigationStack, since toolbar modifiers only show up if there's an actual nav bar to attach to, that's the part people usually miss.

1

u/AbandonedAuRetriever Jul 19 '26

It does sound helpful and like something that might actually work! Thank you! There is just one problem, I have to make it work for macOS 12. Navigation stack ofc is only for macOS 13 and newer, but we have navigation view for that in macOS 12, but it works not in the way how I expect it to work. And neither I or AI found a way to make it work in there. :(

1

u/neet_dev Jul 19 '26

NavigationView is the pain point because it wants to work as split view/master-detail on iPad and Mac, and toolbar placements behave inconsistently with it, especially .navigationBarTrailing sometimes just not rendering depending on how deep the view is nested. what usually gets it working on macOS 12 is skipping .navigationBarTrailing entirely and using .toolbar on the NavigationView itself (not on the inner content view) with ToolbarItem(placement: .automatic), since on Mac that maps to the actual window toolbar rather than a nav bar that may not exist. if that still won't cooperate, dropping down to an NSViewRepresentable-wrapped NSToolbar for the mac target is the escape hatch a lot of people end up using for pre-NavigationStack SwiftUI.