You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SwiftUI 导航栏Menu Picker如何仅展示图标不显示选中项文字

你可以通过指定Picker的样式为MenuPickerStyle实现需求,该样式会优先展示你设置的label内容,不会自动渲染当前选中的选项文本,修改后的代码如下:

.toolbar {
    ToolbarItem(placement: .navigationBarTrailing, content: {
        Picker(selection: $requestHistoryFilter, content: {
            ForEach(SongRequestsContainer.SongHistoryFilter.allCases, id: \.self) {
                Text($0.displayText())
            }
        }, label: {
            Image(systemName: "line.horizontal.3.decrease.circle")
                // 可选:调整图标大小适配导航栏显示
                .font(.title2)
        })
        // 核心修改:指定Picker样式为菜单样式
        .pickerStyle(.menu)                          
    })
}

如果你的项目需要兼容iOS 13版本,可以改用Menu组件包裹Picker实现同样效果,参考代码如下:

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

内容的提问来源于stack exchange,提问作者Pedro Cavaleiro

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 20:27:01