【问题标题】:NavigationBar not showing when going to another tab in tabview导航栏在标签视图中转到另一个标签时不显示
【发布时间】:2020-04-24 03:01:58
【问题描述】:

在我的应用程序中,当我导航到另一个选项卡并向下滚动视图并被截断并且导航栏没有折叠时,如下所示:

我尝试输入.edgesIgnoringSafeArea(.top),但后来发生了这种情况:

当我按下模拟器的主页按钮并导航回应用程序时,导航栏会在按预期滚动时折叠。这是 Xcode 的错误吗?目前我正在使用 XCode 11.4.1,在 iPhone 11 Pro Max 模拟器上进行测试,但在我的物理 iPhone 6s Plus 上出现完全相同的结果。

编辑:标签视图的代码如下:

import SwiftUI

struct MenuScreen: View {
    @State private var selection = 0
    var body: some View {

        TabView(selection: $selection){
            ItemsTab().tabItem{
                Image(systemName: "phone.fill")
                Text("Items")
            }.tag(0)
            TestTab().tabItem{
                Image(systemName: "phone.fill")
                Text("Test")
            }.tag(1)
        }
        .navigationBarTitle("Menu")
//        .edgesIgnoringSafeArea(.top)
        .navigationBarItems(trailing: NavigationLink(destination:ProfileScreen()){Text("Profile")})
//        .padding(.top,1)
            .navigationViewStyle(DefaultNavigationViewStyle())
//        .navigationBarHidden(true)
    }
}

struct MenuScreen_Previews: PreviewProvider {
    static var previews: some View {
        MenuScreen()
    }
}

导航视图被包裹在一个启动屏幕中,如下所示:

import SwiftUI

struct SplashScreen: View {
    @State private var isActive = false
    let content = ContentView()
    var body: some View {

        NavigationView{
            VStack{
                Text("Loading")
                LoopingAnimation()
                NavigationLink(destination: content,isActive: $isActive,label: {EmptyView()})
            }.onAppear(perform: {
                self.goToContentView(time:2.5)
                }).navigationBarTitle("My app")
        }
    }
    func goToContentView(time:Double){
        DispatchQueue.main.asyncAfter(deadline: .now() + Double(time)){
            self.isActive = true
        }
    }
}

struct SplashScreen_Previews: PreviewProvider {
    static var previews: some View {
        SplashScreen()
    }
}

编辑 2:我尝试将导航视图放在 tabview 中,如下所示:

import SwiftUI

struct MenuScreen: View {
    var body: some View {
        TabView{
            NavigationView{
                ItemsTab().navigationBarTitle("Items")
            }.tabItem{
                    Image(systemName: "house.fill")
                    Text("Items")
            }
            NavigationView{
                TestTab().navigationBarTitle("Test")
            }.tabItem{
                Image(systemName: "phone.fill")
                Text("Test")
            }


        }
                        .navigationBarHidden(true)
            //            .navigationBarBackButtonHidden(true)
            .frame(alignment: .center)
//                    .edgesIgnoringSafeArea(.top)
            .navigationBarItems(trailing: NavigationLink(destination:ProfileScreen()){Text("Profile")})
            //        .padding(.top)
            .navigationViewStyle(DefaultNavigationViewStyle())



    }
}

struct MenuScreen_Previews: PreviewProvider {
    static var previews: some View {
        MenuScreen()
    }
}

但是,虽然它导致了一个折叠的导航栏,甚至可以切换标签,结果看起来像这样:

【问题讨论】:

  • 你能把代码贴在这里吗?
  • 您是以编程方式显示视图控制器还是使用情节提要?
  • 当我从父视图中隐藏导航栏时会发生这种情况。是你做的吗?
  • 我正在以编程方式使用 swiftui。
  • 是因为我包裹导航视图的方式吗?

标签: ios xcode scroll swiftui tabview


【解决方案1】:

通过将.displayMode inside .navigationBarTitle 设置为.inline,使导航栏在标签屏幕中保持折叠状态,类似于导航到应用商店标签内的其他类别时发生的情况

截图:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-23
    • 2015-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多