是否有一个很好的解决方案可以在导航视图内使用TabView?

发布于 2025-02-06 03:05:07 字数 1876 浏览 1 评论 0 原文

首先,我知道,对于一个良好的工作环境,NavigationView应该在TabView内部,但是我必须在进入Tabbarview之前先声明NavigationView几个步骤,因为首先您必须完成注册,这需要NavigationView 。

问题是在注册后,TabView中的NavigationBar不起作用。 (要么总是显示相同的导航栏,要么根本没有显示)

所以我将向您展示我的代码现在的外观:

这是 myApp 查看我声明NavigationView的位置:

@main
struct MyApp: App {
    var body: some Scene {
        WindowGroup {
            NavigationView{
                StartView()
            }
        }
    }
}

然后我们从 startView

struct StartView: View {

   VStack{
      //View to Login
      NavigationLink(destination: LoginView(), label: {
          Text("Login View")
      })

      //View to Registration
      NavigationLink(destination: RegistrationView(), label: {
          Text("Registration View")
      })
   }
}

然后,我启动注册程序,您可以通过不同的视图

在此过程中自定义应用程序,以自定义应用程序,我在 mainView (带有带有的视图)上navigationLink tabar)在用户完成其注册 /登录< / strong>时显示。

navigationLink(目标:mainView(),label:{text(“ to Main View”)})

,然后这是 mainView ,其中Navigation Bars不起作用正如他们应该应该的:

struct MainView: View {
    
    //Tab bar color
    init() {
        UITabBar.appearance().backgroundColor = UIColor.white
    }
    
    var body: some View {
        
        TabView(){
            
            //First
            FirstView()
                .tabItem{
                    Image(systemName: "person")
                }
            
            //Second
            SecondView()
                .tabItem{
                    Image(systemName: "person")
                }
            
            //Third
            ThirdView()
                .tabItem{
                    Image(systemName: "person")
                }
        }
    }
}

我现在浏览了很多东西,没有看到任何东西,这对我有帮助,而无需重塑整个代码。我希望有人可以帮助我。谢谢

At first I know that for a good working environment the NavigationView should be inside the TabView, but the thing is I have to declare the NavigationView a few steps before you come to the TabBarView because first you have to complete a registration, which requires the NavigationView.

The problem is after the registration the NavigationBar in the TabView doesn't work.
(Either the same NavigationBar is always displayed or none at all)

So for the beginning I will show you how my code looks right now:

This is the MyApp View where I declare the NavigationView:

@main
struct MyApp: App {
    var body: some Scene {
        WindowGroup {
            NavigationView{
                StartView()
            }
        }
    }
}

Then we start with the StartView:

struct StartView: View {

   VStack{
      //View to Login
      NavigationLink(destination: LoginView(), label: {
          Text("Login View")
      })

      //View to Registration
      NavigationLink(destination: RegistrationView(), label: {
          Text("Registration View")
      })
   }
}

Then I start the registrationProcess where you go through different views to customise the app

On the last View of this process I have a NavigationLink to the MainView (The View with the TabBar) which is shown when the User is finish with his registration / logged in.

NavigationLink(destination: MainView(), label: { Text("To the Main View") })

And then this is the MainView, where the NavigationBars don't work as they should:

struct MainView: View {
    
    //Tab bar color
    init() {
        UITabBar.appearance().backgroundColor = UIColor.white
    }
    
    var body: some View {
        
        TabView(){
            
            //First
            FirstView()
                .tabItem{
                    Image(systemName: "person")
                }
            
            //Second
            SecondView()
                .tabItem{
                    Image(systemName: "person")
                }
            
            //Third
            ThirdView()
                .tabItem{
                    Image(systemName: "person")
                }
        }
    }
}

I browsed a lot now and didn't see anything, which would have helped me without remodelling my whole code. I hope someone can help me. Thanks

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

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

发布评论

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

评论(1

婴鹅 2025-02-13 03:05:08

多亏了 @paulw11的评论,我遇到了以下YouTube视频,并将其包含的代码集成到了我的项目中:更改root视图swiftui

在我对注册过程的最后视图中,我创建了一个按钮,该按钮将我导航到我的主视点(带有navigationlinks的tabview):

Button(action: {
                let window = UIApplication
                    .shared
                    .connectedScenes
                    .flatMap{ ($0 as? UIWindowScene)?.windows ?? [] }
                    .first { $0.isKeyWindow }
                
                window?.rootViewController = UIHostingController(rootView: MainView())
                
                window?.makeKeyAndVisible()
                
            }, label: {
                Text("To the Main View")
            })

然后我更改了我的主视图:

struct MainView: View {
    
    //Tab bar color
    init() {
        UITabBar.appearance().backgroundColor = UIColor.white
    }
    
    var body: some View {
        
        TabView(){
            
            //First
            NavigationView{ FirstView() }
                .tabItem{
                    Image(systemName: "person")
                }
            
            //Second
            NavigationView{ SecondView() }
                .tabItem{
                    Image(systemName: "person")
                }
            
            //Third
            NavigationView{ ThirdView() }
                .tabItem{
                    Image(systemName: "person")
                }
        }
    }
}

Thanks to @Paulw11's comment, I came across the following Youtube video and integrated the code it contains into my project: Change root view SwiftUI

In my last View of the Registration Process I created a Button which navigated me to my MainView (TabView with NavigationLinks):

Button(action: {
                let window = UIApplication
                    .shared
                    .connectedScenes
                    .flatMap{ ($0 as? UIWindowScene)?.windows ?? [] }
                    .first { $0.isKeyWindow }
                
                window?.rootViewController = UIHostingController(rootView: MainView())
                
                window?.makeKeyAndVisible()
                
            }, label: {
                Text("To the Main View")
            })

And then I changed my MainView to this:

struct MainView: View {
    
    //Tab bar color
    init() {
        UITabBar.appearance().backgroundColor = UIColor.white
    }
    
    var body: some View {
        
        TabView(){
            
            //First
            NavigationView{ FirstView() }
                .tabItem{
                    Image(systemName: "person")
                }
            
            //Second
            NavigationView{ SecondView() }
                .tabItem{
                    Image(systemName: "person")
                }
            
            //Third
            NavigationView{ ThirdView() }
                .tabItem{
                    Image(systemName: "person")
                }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文