UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: How to hide a custom tabbar on a detail view?

Forums > SwiftUI

I created the custom tabbar like below and I need to hide it only on the DetailView which is below the MyLibraryView in hierarchy.

What would be the best way to achieve that? I tried to come up with several approaches but they all seemed overly complicated and couldn't make them work well in the end.

This is the MainView:

import SwiftUI

struct MainView: View {

    //        @State var selectedTab: Tabs = .home

    @State var selectedIndex = 0

    var body: some View {

        ZStack {
            if selectedIndex == 0 {
                HomeView()
            } else if selectedIndex == 1 {
                MyLibraryView()
            } else if selectedIndex == 2 {
                BrowseView()
            } else if selectedIndex == 3 {
                ProfileView()
            } 

            VStack {
                Spacer()
                    CustomTabBarView(selectedIndex: $selectedIndex)
            }

        }
    }
}

And this is the CustomTabView:


import SwiftUI

struct CustomTabBarView: View {

    @Binding var selectedIndex : Int

    let tabBarImageNames = ["house", "rectangle.stack", "square.grid.2x2","person.fill" ]
    let tabBarNames = ["Home", "My library", "Browse", "Profile"]

    var body: some View {

        VStack {
            HStack (){
                Spacer()
                Spacer()
                ForEach(0..<4) { num in
                    Button(action: {
                        selectedIndex = num
                    }, label: {
                        Spacer()
                        VStack  {
                            Image(systemName: tabBarImageNames[num])
                                .font(.system(size: 20))
                                .frame(width: 20,height: 20)
                                .foregroundColor(selectedIndex == num ? Color(.black) : .init(white: 0.6))
                                .scaledToFit()
                            Text (tabBarNames[num])
                                .padding(.top,2)
                                .font(.system(size: 12))
                                .foregroundColor(selectedIndex == num ? Color(.black) : .init(white: 0.6))
                        }

                        Spacer()
                    })
                }
            }

            .ignoresSafeArea()
            .frame(height: 70)
            .background(.white.opacity(0.85))
        }
        .background(.ultraThinMaterial)
        .cornerRadius(30)
        .padding(.horizontal,20)
        .if(!hasNotch()) { view in
            view.padding(.bottom,10)
        }
        .shadow(color: Color.black.opacity(0.05), radius: 10)
    }
}

2      

Here a couple of solutions

IF BLOCK

Wrap the CustomTabBarView in a if block

struct ContentView: View {
    @State var selectedIndex = 0

    var body: some View {

        ZStack {
            if selectedIndex == 0 {
                HomeView()
            } else if selectedIndex == 1 {
                MyLibraryView(selectedIndex: $selectedIndex)
            } else if selectedIndex == 2 {
                BrowseView()
            } else if selectedIndex == 3 {
                ProfileView()
            }

            if selectedIndex != 1 {
                VStack {
                    Spacer()
                    CustomTabBarView(selectedIndex: $selectedIndex)
                }
            }
        }
    }
}

PS you have to have a Button in the MyLibraryView to return.

struct MyLibraryView: View {
    @Binding var selectedIndex : Int

    var body: some View {
        VStack {
            Text("My Library View")
              .padding()

            Button("Home") {
                selectedIndex = 0
            }
        }
    }
}

SHEET VIEW

Use a fullScreenCover to overlay the MyLibraryView need to amend the CustomTabBarView

struct CustomTabBarView: View {
    @Binding var selectedIndex : Int
    @Binding var showMyLibraryView: Bool //<- add this

    let tabBarImageNames = ["house", "rectangle.stack", "square.grid.2x2","person.fill" ]
    let tabBarNames = ["Home", "My library", "Browse", "Profile"]

    var body: some View {

        VStack {
            HStack (){
                Spacer()
                Spacer()
                ForEach(0..<4) { num in
                    Button(action: { // <- Change the action
                        if num == 1 {
                            showMyLibraryView = true
                        } else {
                            selectedIndex = num
                        }
                    }, label: {
                        Spacer()
                        VStack  {
                            Image(systemName: tabBarImageNames[num])
                                .font(.system(size: 20))
                                .frame(width: 20,height: 20)
                                .foregroundColor(selectedIndex == num ? Color(.black) : .init(white: 0.6))
                                .scaledToFit()
                            Text (tabBarNames[num])
                                .padding(.top,2)
                                .font(.system(size: 12))
                                .foregroundColor(selectedIndex == num ? Color(.black) : .init(white: 0.6))
                        }

                        Spacer()
                    })
                }
            }

            .ignoresSafeArea()
            .frame(height: 70)
            .background(.white.opacity(0.85))
        }
        .background(.ultraThinMaterial)
        .cornerRadius(30)
        .padding(.horizontal,20)
//        .if(!hasNotch()) { view in
//            view.padding(.bottom,10)
//        }
        .shadow(color: Color.black.opacity(0.05), radius: 10)
    }
}

Then you can change the main view to

struct ContentView: View {
    @State var selectedIndex = 0
    @State private var showMyLibraryView = false

    var body: some View {

        ZStack {
            if selectedIndex == 0 {
                HomeView()
            } else if selectedIndex == 2 {
                BrowseView()
            } else if selectedIndex == 3 {
                ProfileView()
            }

            VStack {
                Spacer()
                CustomTabBarView(selectedIndex: $selectedIndex, showMyLibraryView: $showMyLibraryView)
            }
        }
        .fullScreenCover(isPresented: $showMyLibraryView) {
            MyLibraryView()
        }
    }
}

PS you need to change the MyLibraryView to dismiss the sheet

struct MyLibraryView: View {
    @Environment(\.dismiss) var dismiss

    var body: some View {
        VStack {
            Text("My Library View")
              .padding()

            Button("Dismiss") {
               dismiss()
            }
        }
    }
}

2      

Hi @NigelGee, Thank you for your suggestions, but I think they don't accomplish what I need (maybe I needed to be a little more clear). I do need the tabbar in the MyLibraryView. But inside the MyLibraryView there is a list and if you tap an item you get into the Detailview. And the DetailView is where I don't need the tab bar. So this DetailView is like a separate page and that's the only place where I don't need the tab bar.

2      

It would help if you give the correct information. I have done this but it only guessing what you want but this keep the tab bar in view until the detail view is shown

struct ContentView: View {
    @State var selectedIndex = 0
    @State private var showTabBar = true // <- Controls if TabBar is visable

    var body: some View {

        ZStack {
            if selectedIndex == 0 {
                HomeView()
            } else if selectedIndex == 1 {
                MyLibraryView(showTabBar: $showTabBar) // <- pass in control to the view
            } else if selectedIndex == 2 {
                BrowseView()
            } else if selectedIndex == 3 {
                ProfileView()
            }

            if showTabBar {
                VStack {
                    Spacer()
                    CustomTabBarView(selectedIndex: $selectedIndex)
                }
            }
        }
    }
}
struct MyLibraryView: View {
    let data = ["One", "Two", "Three", "Four", "Five", "Six"] // <- list of made up data for list

    @Binding var showTabBar: Bool // <- pass though Binding

    var body: some View {
        NavigationView {
            List {
                ForEach(data, id: \.self) { input in
                    NavigationLink(destination: DetailView(input: input, showTabBar: $showTabBar)) {
                        Text(input)
                    }
                }
            }
            .navigationTitle("My Library")
        }
    }
}
struct DetailView: View {
    var input: String
    @Binding var showTabBar: Bool

    var body: some View {
        Text(input)
            .onAppear { // <- when view appears TabBar is not visable
                showTabBar = false
            }
            .onDisappear {
                showTabBar = true // <- when view is dismissed then TabBar is visable
            }
    }
}

3      

Thank you so much @NigelGee that works perfectly!

I did mention it, but I could have been more clear and will be in the future.

2      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.