How to hide the TabBar when navigate with NavigationLink in SwiftUI?
Asked Answered
N

3

27

I have a TabView with 2 tabs in it, each tab containing a NavigationView. I need to hide the TabBar when navigating to another view. One solution would be to place the TabView inside of one NavigationView, but I have to set different properties for each NavigationView.

TabView(selection: $selectedTab, content: {
            NavigationView {
                VStack {
                    NavigationLink(destination: Text("SecondView Tab1")) {
                        Text("Click")
                    }
                }
            }.tabItem {
                Text("ONE")
            }.tag(0)

            NavigationView {
                VStack {
                    NavigationLink(destination: Text("SecondView Tab2")) {
                        Text("Click")
                    }
                }
            }.tabItem {
                Text("TWO")
            }.tag(1)

        })

P.S. I am using Xcode 11 Beta 5

Niobe answered 1/8, 2019 at 8:30 Comment(2)
Check this out github.com/smartvipere75/bottombar-swiftuiHolmberg
I posted one way to solve this limitation here: https://mcmap.net/q/534955/-hide-tabview-after-clicking-on-a-navigationlink-in-swiftuiMarsha
M
28

A little late but it will work, put your NavigationView before the TabView and the tab buttons are going to be hidden when you use a navigation link in your tabbed views.

NavigationView{
    TabView{
        ...
    }
}
Monocyte answered 12/12, 2019 at 12:28 Comment(3)
best answer I foundInroad
This has unintended consequences https://mcmap.net/q/534956/-swiftui-tabbar-persists-navigationbartitle-position-in-tab-39-s-views/196555Douceur
no longer valid after ios17Asmodeus
A
1

I have same problem for this; And I did the following actions to solve this problem:

  1. Use NavigationView Contain a TabView And Hidden the NavigationBar
  2. Make a Custom NavigaitonView like this
  3. In next view Still hidden NavigationBar
// root tab
NavigationView {
    TabView {
        // some
    }
    .navigationBarTitle(xxx, displayMode: .inline)
    .navigationBarHidden(true)
}
// custom navigation view
@available(iOS 13.0.0, *)
struct MyNavigationView: View {
    var body: some View {
        HStack {
            Spacer()
            Text(some)
            Spacer()
        }
        .frame(height: 44)
    }
}
// this view 
VStack {
                MyNavigationView()
                Image(some)
                    .resizable()
                    .frame(width: 100, height: 100, alignment: .top)
                    .padding(.top, 30)
                Spacer()
                HStack {
                    ClockView()
                    Spacer()
                    NavigationLink(
                        destination: DynamicList(),
                        label: {
                            Image(some)
                        }).navigationBarHidden(true)
                }
                .padding(EdgeInsets(top: 0, leading: 15, bottom: 0, trailing: 15))
                
                Spacer()
            }
// next view
var body: some View {
            VStack {
                List {
                    MyNavigationView()
                    ForEach(date, id: \.self) { model in
                        Text(model)
                    }
                }
                .navigationBarHidden(true)
                .navigationBarTitle(some, displayMode: .inline)
            }
    }
Armand answered 4/8, 2020 at 11:3 Comment(0)
L
-2

You can't hide the tab bar as far as I know if you navigation view its listed as a child, your tab bar contains your navigation view.

Lonna answered 1/8, 2019 at 20:51 Comment(1)
I didn't downvote this myself, but my guess is that this is downvoted because this option is available in UIKit, so this behaviour should be available in SwiftUILiesa

© 2022 - 2024 — McMap. All rights reserved.