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

SwiftUI嵌套TabView与NavigationStack时ToolbarItem异常求助

SwiftUI NavigationStack嵌套TabView时ToolbarItem叠加异常

问题现象

  • 预期:选中不同标签页时,导航栏右侧的ToolbarItem会切换为对应标签页的自定义按钮
  • 实际:切换标签页后,ToolbarItem会在导航栏右侧不断叠加,重复显示多个相同按钮

相关代码

主视图代码

@State var selectedIndex: Int
@State var path = NavigationPath()

var body: some View {
    ZStack {
        NavigationStack(path: $path) {
            TabView(selection: $selectedIndex) {
                Group {
                    TextView(tabName: .constant("Home"))
                        .tabItem {
                            Label("Home", systemImage: "house")
                        }
                        .tag(1)
                    TextView(tabName: .constant("Contacts"))
                        .tabItem {
                            Label("Contacts", systemImage: "person.3")
                        }
                        .tag(1)
                    TextView(tabName: .constant("Settings"))
                        .tabItem {
                            Label("Settings", systemImage: "gearshape")
                        }
                        .tag(1)
                }
                .toolbarBackground(Color.red)
                .toolbarBackground(.visible, for: .navigationBar)
                .toolbarColorScheme(.dark, for: .navigationBar)
            }
        }
    }
}

标签页视图代码(已简化)

@Binding var tabName: String

var body: some View {
    Text("Tab \(tabName)")
        .toolbar {
            ToolbarItem(placement: .navigationBarTrailing) {
                Button {
                    print("Profile ")
                } label: {
                    Image(systemName: "person.circle")
                }
            }
        }
}

问题截图

  • 标签页1:
    Screenshot of Tab 1
  • 标签页2:
    Screenshot of Tab 2
  • 标签页3:
    Screenshot of Tab 3

已查阅Apple开发者文档未找到解决方案,寻求技术支持。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 10:01:13