简体   繁体   English

Swiftui NavigationView + TabView 不显示导航栏项目

[英]Swiftui NavigationView + TabView doesn't show navbar item

I have four Views inside a TabView and each of them contains NavigationView with title.我在TabView中有四个视图,每个视图都包含带有标题的NavigationView However, when the view first shows up, the navigation view does not show as designed.但是,当视图首次显示时,导航视图并未按设计显示。

Even though I have the navigation bar item, the view would always be a blank child view.即使我有导航栏项目,该视图也将始终是一个空白的子视图。 It is only when I click to another page and then coming back to the navigation view that the view would show normally.只有当我单击另一个页面然后返回导航视图时,该视图才会正常显示。 What could be the problems?可能是什么问题?

Attached is the screenshot of the preview page.附上预览页面截图。 Thanks in advance.提前致谢。

struct MainContentView: View {
    @State private var navSelection = 0
    
    var body: some View {
        
        VStack(alignment: .center){
          
            TabView(selection:$navSelection){
                NavigationView{
                    HomeView()
                        .navigationBarItems(leading: Text("Title").font(.system(size:24,weight: .heavy)), trailing: Image(systemName: "bell.fill"))
                        .navigationViewStyle(StackNavigationViewStyle())
                       
                }.tag(0)
                
                NavigationView{
                    ExploreView()
                       
                }.tag(1)
        
                Text("Post").tag(2)
                Text("Market").tag(3)
                Text("Account").tag(4)
            }.tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
                .disabled(true)
            
            MainTabBarView(navSelection: self.$navSelection)
            }
    }
}

在此处输入图像描述

The preview sample预览示例

Just a small mistake, apply the style to the navigation view as follows:只是一个小错误,将样式应用到导航视图如下:

NavigationView{ 
}
.navigationViewStyle(.stack)
         

What went wrong is some how SwiftUI found a detail view somewhere in the hierarchy and pushed it because normally inside NavigationView there are two views defined but you only defined one.出了问题的是 SwiftUI 如何在层次结构中的某处找到详细视图并将其推送,因为通常在 NavigationView 中定义了两个视图,但您只定义了一个。 I think .tabViewStyle is what caused it.我认为.tabViewStyle是导致它的原因。

Also to set the nav item title we use .navigationTitle("Home")还要设置导航项标题,我们使用.navigationTitle("Home")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM