SwiftUI嵌套TabView与NavigationStack时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:

- 标签页2:

- 标签页3:

已查阅Apple开发者文档未找到解决方案,寻求技术支持。
内容的提问来源于stack exchange,提问作者ColinMasters
相关产品推荐
相关产品推荐

