Understanding SwiftUI toolbars

I have two apps written in SwiftUI. Both use a NavigationSplitView as their primary content with a list-based sidebar. Both apps have a few toolbar items offering functionality relevant to the application purpose. Both sidebars have toolbar items, declared using the .toolbar view modifier, that add ToolbarItems to their view (a "filter" menu). Both the apps' main content view use .toolbar to add some ToolbarItems to the main content toolbar as well. Both also include the SidebarCommands() command group.

My question is this: in one app, when I click the Hide Sidebar button, the toolbar items that were attached to the sidebar move to the main toolbar. In my second app, the toolbar items move to the "More" disclosure menu at the end of the toolbar.

I want behavior like the first app (where the toolbar items end up in the leading edge of the main toolbar) in the second app, but I've scrubbed through both applications' toolbar code, I've had coding assistants compare the two, I've tried stripping down the toolbar items to remove all but the Hide Sidebar. Nothing I can discover shows me why one app moves the toolbar items to the main toolbar up front, while the other moves them to the back.

Am I missing something about how to declare toolbar items? Is there some visibility priority thing I’m missing?

Can you show the relevant code for the toolbars?

Are you missing something like:

ToolbarItemGroup(placement: .primaryAction) {
    // toolbar items
}

Here is the SidebarView toolbar from the app that behaves.

.toolbar {
            ToolbarItemGroup(placement: .automatic) {
                displayModeToggle
                sortMenu
            }
        }

Here is the main content toolbar from the app that behaves.

@ToolbarContentBuilder
    private var windowToolbarContent: some ToolbarContent {
        if selectedPhoto != nil {
            ToolbarItem(placement: .navigation) {
                toolbarTitleCapsule
            }
        }

        ToolbarItemGroup(placement: .primaryAction) {
            lockToggleButton
            removeLockButton

            if selectedPhoto == nil, !showingSlideshow, !(collectionManager.isSelectedCollectionPasswordProtected && collectionManager.isLocked), !filteredPhotos.isEmpty {
                Button {
                    detailPhotoID = filteredPhotos.first?.id
                    showingSlideshow = true
                } label: {
                    Label("Slideshow", systemImage: "play.fill")
                }
            }
        }
    }

Here is the Sidebar toolbar from the app that misbehaves.

        .toolbar {
            ToolbarItemGroup(placement: .automatic) {
                sortMenu
            }
        }

Here is the main content toolbar from the app that misbehaves.

    @ToolbarContentBuilder
    private var windowToolbarContent: some ToolbarContent {
        ToolbarItem(placement: .navigation) {
            roomDirectoryButton
        }

        if selectedRoomId != nil {
            ToolbarItem(placement: .secondaryAction) {
                toolbarTitleCapsule
            }
        }
        
        ToolbarItem(placement: .primaryAction) {
            showInspectorButton
        }
    }

Both apps are open source. Their code is up on GitHub.

What does displayModeToggle do ?

In the misbehaving code, I would also try to simplify, in order to localise the cause of error:

    @ToolbarContentBuilder
    private var windowToolbarContent: some ToolbarContent {
        ToolbarItem(placement: .navigation) {
            roomDirectoryButton
        }
 
    /* comment out
        if selectedRoomId != nil {
            ToolbarItem(placement: .secondaryAction) {
                toolbarTitleCapsule
            }
        } */
        
        ToolbarItem(placement: .primaryAction) {
            showInspectorButton
        }
    }

As this is code you reuse, did you ask to the code author ?

So could you post complete code so that we can test on our own ?

Which version of Xcode ? Is it on simulator or device ? In this case, which OS version ?

Understanding SwiftUI toolbars
 
 
Q