7

when I have a TabView{} and the first Tab has a NavigationView, when I click on a Row, I want that TabView{} to disappear. How do I do that?

Same Issue here: How to hide the TabBar when navigate with NavigationLink in SwiftUI?

But unfortunately no solution.

Enea Dume
  • 3,014
  • 3
  • 21
  • 36
seref
  • 543
  • 2
  • 6
  • 19

4 Answers4

1

There is no way to do that currently. For example, NavigationView responds to the .navigationBarHidden(_:) method on its descendants, but there is not an equivalent for TabView.

If this is something you'd like to see, let Apple know.

John M.
  • 8,892
  • 4
  • 31
  • 42
1

there's no way to hide TabView so I had to add TabView inside ZStack as this:

var body: some View {
    ZStack {
        TabView {
            TabBar1().environmentObject(self.userData)
                .tabItem {
                    Image(systemName: "1.square.fill")
                    Text("First")
            }
            TabBar2()
                .tabItem {
                    Image(systemName: "2.square.fill")
                    Text("Second")
            }
        }

        if self.userData.showFullScreen {
            FullScreen().environmentObject(self.userData)

        }
    }
}

UserData:

  final class UserData: ObservableObject {
    @Published var showFullScreen = false}

TabBar1:

struct TabBar1: View {
    @EnvironmentObject var userData: UserData

    var body: some View {
        Text("TabBar 1")
            .edgesIgnoringSafeArea(.all)
            .frame(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
            .background(Color.green)
            .onTapGesture {
                self.userData.showFullScreen.toggle()
        }
    }
}

FullScreen:

struct FullScreen: View {
    @EnvironmentObject var userData: UserData

    var body: some View {
        Text("FullScreen")
            .edgesIgnoringSafeArea(.all)
            .frame(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
            .background(Color.red)
            .onTapGesture {
                self.userData.showFullScreen.toggle()
        }
    }
}

check full code on Github

there's also some other ways but it depends on the structure of the views

fakiho
  • 521
  • 4
  • 13
  • Good workaround! Just one thing if someone struggles to implement your above code: In SceneDelegate you have to add the environmentObject to your rootViewController like this: window.rootViewController = UIHostingController(rootView: contentView.environmentObject(UserData())) – Kuhlemann Apr 07 '20 at 14:49
1

To solve this limitation, I came out with this approach:

  • Created an enum to identify the tabs
enum Tabs: Int {
    case tab1
    case tab2
    
    var title: String {
        switch self {
        case .tab1: return "Tab 1 Title"
        case .tab2: return "Tab 2 Title"
        }
    }

    var imageName: String {
        switch self {
        case .tab1: return "star" // Example using SF Symbol
        case .tab2: return "ellipsis.circle"
        }
    }
}
  • Inside the view, such as ContentView.swift, added a property like this:
@State private var selectedTab = Tabs.tab1
  • Inside the body:
NavigationView {
    TabView(selection: $selectedTab) {
        ViewExample1()
            .tabItem {
                Image(systemName: Tabs.tab1.imageName)
                Text(Tabs.tab1.title)
        }.tag(Tabs.tab1)
        
        ViewExample2()
            .tabItem {
                Image(systemName: Tabs.tab2.imageName)
                Text(Tabs.tab2.title)
        }.tag(Tabs.tab2)
    }
    .navigationBarTitle(selectedTab.title)
}

That's all. I hope this might be helpful.

Note: Just be aware this workaround hides the TabView in any and all child views, if you want to hide in just a particular view, this won't give you the result that you looking for.

Hopefully, Apple implements an (official and proper) option to hide the TabView soon.

Rod
  • 11
  • 3
  • This works when you enter a child view, but when you return to the parent view at the top of the hierarchy, the TabView disappears. This is in ios 14.4 with Xcode 12.3 – Andre Feb 02 '21 at 02:19
-1

Don't Use NavigationLink

1

First Define this

2

Later use this

3

Change myFullScreenCover & isTabViewHidden Sorry

Larx
  • 1
  • 1
  • https://meta.stackoverflow.com/a/285557/7733418 will explain what the problem with your post is. – Yunnosch Aug 26 '23 at 19:29
  • 1
    Please add code and data as text ([using code formatting](/editing-help#code)), not images. Images: A) don't allow us to copy-&-paste the code/errors/data for testing; B) don't permit searching based on the code/error/data contents; and [many more reasons](//meta.stackoverflow.com/a/285557). Images should only be used, in addition to text in code format, if having the image adds something significant that is not conveyed by just the text code/error/data. – M-- Aug 28 '23 at 05:32