Swiftui - 正确处理根导航内的选项卡导航

发布于 2025-01-15 10:01:08 字数 806 浏览 4 评论 0原文

我的 swiftUI 应用程序导航的结构如下所示,

View : A
{

Navigation View {
     // List View on click 
     // Takes me to a Tab View
     NavigationLink(destination : Tab View)

}

}
View : Tab View
{
       ViewX 
            .tag(0)
            .tabItem {
                Image(systemName: "video.bubble.left.fill")
                Text("View X")
                    .font(Font.custom("Roboto-Black", size: 30))
            }
       ViewY 
            .tag(0)
            .tabItem {
                Image(systemName: "video.bubble.left.fill")
                Text("View Y")
                    .font(Font.custom("Roboto-Black", size: 30))
            }

}

使用此结构我无法正确控制视图的导航标题。 如果我将每个选项卡项包装在导航视图中,我最终会按预期得到多个导航标题栏。 是否有任何特定方法(例如隐藏根导航栏)可以在嵌套视图中拥有一个具有适当标题更新的导航栏?

The structure of my swiftUI app navigation is as below

View : A
{

Navigation View {
     // List View on click 
     // Takes me to a Tab View
     NavigationLink(destination : Tab View)

}

}
View : Tab View
{
       ViewX 
            .tag(0)
            .tabItem {
                Image(systemName: "video.bubble.left.fill")
                Text("View X")
                    .font(Font.custom("Roboto-Black", size: 30))
            }
       ViewY 
            .tag(0)
            .tabItem {
                Image(systemName: "video.bubble.left.fill")
                Text("View Y")
                    .font(Font.custom("Roboto-Black", size: 30))
            }

}

With this structure I'm not able to control the navigation title of the view correctly.
If I wrap each tab item in a navigation view, I end up with multiple navigation title bars as expected.
Any particular approach (like hiding the root navigation bar) to have one navigation bar with appropriate title updates in nested views ?

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

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

发布评论

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

评论(1

記柔刀 2025-01-22 10:01:08

您将无法自动更新导航标题,您需要执行以下操作:

import SwiftUI

enum Tabs: String {
    case view1
    case view2
}

struct tab: View {

@State var activeTab = Tabs.view1

var body: some View {
    TabView(selection: $activeTab)
    {
        Text("View1")
            .tag(Tabs.view1)
                .tabItem {
                    Image(systemName: "video.bubble.left.fill")
                    Text("View X")
                        .font(Font.custom("Roboto-Black", size: 30))
                }
               
        Text("View2")
            .tag(Tabs.view2)
                .tabItem {
                    Image(systemName: "video.bubble.left.fill")
                    Text("View Y")
                        .font(Font.custom("Roboto-Black", size: 30))
                }
             

    }
    .navigationTitle("Active View: \(activeTab.rawValue)")
}
}

在此处输入图像描述

you won't be able to update the navigation Title automatically you will need to do something like this:

import SwiftUI

enum Tabs: String {
    case view1
    case view2
}

struct tab: View {

@State var activeTab = Tabs.view1

var body: some View {
    TabView(selection: $activeTab)
    {
        Text("View1")
            .tag(Tabs.view1)
                .tabItem {
                    Image(systemName: "video.bubble.left.fill")
                    Text("View X")
                        .font(Font.custom("Roboto-Black", size: 30))
                }
               
        Text("View2")
            .tag(Tabs.view2)
                .tabItem {
                    Image(systemName: "video.bubble.left.fill")
                    Text("View Y")
                        .font(Font.custom("Roboto-Black", size: 30))
                }
             

    }
    .navigationTitle("Active View: \(activeTab.rawValue)")
}
}

enter image description here

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