ToolbarItem with .hidden() or .opacity(0) leaves a visible frame when built with Xcode 26 (worked fine in Xcode 16.4)

In Xcode 16.4, I could hide a ToolbarItem simply by applying .hidden() or .opacity(0), and the button disappeared completely — including its touch area and layout space.

However, when I build the same code with Xcode 26, the button visually disappears but its frame and tap area remain, as if the ToolbarItem were still present but invisible. This causes an empty tappable area to remain in the toolbar.

Here’s a simplified example:

.toolbar {
    ToolbarItem(placement: .topBarLeading) {
        Button("Back") {
            // action
        }
        .opacity(isSelectionMode ? 0 : 1)
        // or .hidden() – both produce the same issue
    }

    ToolbarItem(placement: .principal) {
        Text("Title")
    }

    ToolbarItem(placement: .topBarTrailing) {
        Button("Select") {
            isSelectionMode.toggle()
        }
    }
}
  • Expected behavior (Xcode 16.4):

 When isSelectionMode becomes true, the Back button disappears completely and its space in the toolbar is released.

  • Actual behavior (Xcode 26):

 The Back button becomes invisible, but its tappable area and layout spacing remain, leaving an empty spot in the toolbar.

To work around this, I tried conditionally including the ToolbarItem only when isSelectionMode == false, like this:

@ToolbarContentBuilder
private var toolbarLeadingContent: some ToolbarContent {
    if !isSelectionMode {
        ToolbarItem(placement: .topBarLeading) {
            Button("Back") { }
        }
    }
}

However, this approach fails to build when the minimum deployment target is iOS 15, showing the compiler error:

'buildIf' is only available in iOS 16.0 or newer

I understand that @ToolbarContentBuilder relies on buildIf internally for conditional branches, which explains the limitation, but this means there’s currently no iOS 15–compatible way to completely remove a ToolbarItem at runtime.

So my questions are:

  1. Is the layout change in Xcode 26 an intentional behavior change in SwiftUI?
  2. What’s the correct way to fully remove a ToolbarItem on iOS 15–compatible projects without using buildIf?
ToolbarItem with .hidden() or .opacity(0) leaves a visible frame when built with Xcode 26 (worked fine in Xcode 16.4)
 
 
Q