Search code examples
xcodeswiftuiios14

SwiftUI Menu Picker Navigation Bar show only icon


I want to have a menu picker on the navigation bar, so far I was able to accomplish that with the following code

.toolbar {
    ToolbarItem(placement: ToolbarItemPlacement.navigationBarTrailing, content: {
        Picker(selection: $requestHistoryFilter, content: {
            ForEach(SongRequestsContainer.SongHistoryFilter.allCases, id: \.self) {
                Text($0.displayText())
            }
        }, label: {
            Image(systemName: "line.horizontal.3.decrease.circle")
        })                          
})

But the code above always outputs the text of the selected option on the navigation bar, what I wanted was to always show the label, is there a way of accomplish that?

In this case was I would like to show always the image.

Is there a way to achieve this?


Solution

  • Following @hayesk suggestion, wrapping using the Menu is the way to go

    Here's what worked for me as reference for others

    .toolbar {
        ToolbarItem(placement: ToolbarItemPlacement.navigationBarTrailing, content: {
            Menu(content: {
                Picker(selection: $requestHistoryFilter, label: Image(systemName: "line.horizontal.3.decrease.circle")) {
                    ForEach(VideoRequestsContainer.VideoHistoryFilter.allCases, id: \.self) {
                        Text($0.displayText())
                    }
                }
            }, label: {
                Image(systemName: "line.horizontal.3.decrease.circle")
            })
         })
    }