在Swift中的特定TABVIEW项目上带有工具栏按钮的NAV栏

发布于 2025-02-07 03:09:03 字数 1118 浏览 2 评论 0原文

我有一个TabView,其中包含三个视图(三角形,正方形和圆圈),嵌套在导航视图和链接中。 TabView工作正常。我想只为特定的tabview提供工具栏按钮;说圈子。工具栏修饰符在所有TabViews上添加了按钮。

struct ContentView: View {
  var body: some View {
    NavigationView {
      NavigationLink() {
        TabView {
          Text("triangle")
            .tabItem {
              Label("triangle", systemImage: "triangle")
            }
          
          Text("square")
            .tabItem {
              Label("square", systemImage: "square")
            }
          
          Text("circle")
            .tabItem {
              Label("circle", systemImage: "circle")
            }
        }
        .navigationTitle("Tabs")
        .toolbar {
          ToolbarItemGroup(placement: .navigationBarTrailing) {
            Button("About") {
              print("About tapped!")
            }
  
            Button("Help") {
              print("Help tapped!")
            }
          }
        }
      } label: {
        Text("Hello!")
      }
      .navigationTitle("Title")
    }
  }
}

如何设置此设置以仅在一个TabView上显示工具栏按钮?

我想一个次要选项(少优选)可能是禁用不需要的tabviews上的按钮(如果可能的话)。

I have a TabView with three views (triangle, square and circle) nested inside a navigation view and link. TabView works fine. I'd like to have toolbar buttons for only a specific tabview; say circle. The toolbar modifier adds the buttons on all the tabviews.

struct ContentView: View {
  var body: some View {
    NavigationView {
      NavigationLink() {
        TabView {
          Text("triangle")
            .tabItem {
              Label("triangle", systemImage: "triangle")
            }
          
          Text("square")
            .tabItem {
              Label("square", systemImage: "square")
            }
          
          Text("circle")
            .tabItem {
              Label("circle", systemImage: "circle")
            }
        }
        .navigationTitle("Tabs")
        .toolbar {
          ToolbarItemGroup(placement: .navigationBarTrailing) {
            Button("About") {
              print("About tapped!")
            }
  
            Button("Help") {
              print("Help tapped!")
            }
          }
        }
      } label: {
        Text("Hello!")
      }
      .navigationTitle("Title")
    }
  }
}

How can I set this up to only show toolbar buttons on one tabview only?

I suppose a secondary option (way less preferred) may be to disable the buttons on tabviews where they are not needed (if possible).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

娇女薄笑 2025-02-14 03:09:03

可以使用TabView的选择状态完成,并根据该状态对特定工具栏按钮进行可见性。

这是一个演示。用Xcode 13.4/ios 15.5

“

struct ContentView: View {
    @State private var selection = 0   // << here !!
    var body: some View {
        NavigationView {
            NavigationLink() {
                TabView(selection: $selection) {     // << here !!
                    Text("triangle")
                        .tabItem {
                            Label("triangle", systemImage: "triangle")
                        }.tag(0)
                    
                    Text("square")
                        .tabItem {
                            Label("square", systemImage: "square")
                        }.tag(1)
                    
                    Text("circle")
                        .tabItem {
                            Label("circle", systemImage: "circle")
                        }.tag(2)
                }
                .navigationTitle("Tabs")
                .toolbar {
                    ToolbarItemGroup(placement: .navigationBarTrailing) {
                        if selection == 2 {                            // << here !!
                            Button("About") {
                                print("About tapped!")
                            }
                            
                            Button("Help") {
                                print("Help tapped!")
                            }
                        }
                    }
                }
            } label: {
                Text("Hello!")
            }
            .navigationTitle("Title")
        }
    }
}

github中的测试代码

It can be done with a selection state for TabView and making visibility of specific toolbar buttons depending on that state.

Here is a demo. Tested with Xcode 13.4 / iOS 15.5

demo

struct ContentView: View {
    @State private var selection = 0   // << here !!
    var body: some View {
        NavigationView {
            NavigationLink() {
                TabView(selection: $selection) {     // << here !!
                    Text("triangle")
                        .tabItem {
                            Label("triangle", systemImage: "triangle")
                        }.tag(0)
                    
                    Text("square")
                        .tabItem {
                            Label("square", systemImage: "square")
                        }.tag(1)
                    
                    Text("circle")
                        .tabItem {
                            Label("circle", systemImage: "circle")
                        }.tag(2)
                }
                .navigationTitle("Tabs")
                .toolbar {
                    ToolbarItemGroup(placement: .navigationBarTrailing) {
                        if selection == 2 {                            // << here !!
                            Button("About") {
                                print("About tapped!")
                            }
                            
                            Button("Help") {
                                print("Help tapped!")
                            }
                        }
                    }
                }
            } label: {
                Text("Hello!")
            }
            .navigationTitle("Title")
        }
    }
}

Test code in GitHub

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文