0

First of all I did a full research for my problem and NOTHING on Google helped me, the problem is simple, it seems that the solution should also be simple, but I can't hide the tabbar in NavigationLink, and if something works out, then wierd behavior of the buttons and the transition back, etc...

TabView itself

import SwiftUI

struct Main: View {
    
    @State var currentTab: Int = 0
    
    var body: some View {
            TabView(selection: $currentTab) {
                HomeView().tag(0)
                AccountInfoView().tag(1)
                SettingsView().tag(2)
            }
            .tabViewStyle(.page(indexDisplayMode: .never))
            .edgesIgnoringSafeArea(.bottom)
            .overlay(
                TabBarView(currentTab: $currentTab),
                alignment: .top
                )
       }
}

struct TabBarView: View {
    
    @Binding var currentTab: Int
    @Namespace var namespace
    
    var tabBarOptions: [String] = ["Search", "Items", "Account"]
    var body: some View {
            HStack(spacing: 0) {
                ForEach(Array(zip(self.tabBarOptions.indices,
                                  self.tabBarOptions)),
                        id: \.0,
                        content: {
                    index, name in
                    TabBarItem(currentTab: self.$currentTab,
                               namespace: namespace.self,
                               tabBarItemName: name,
                               tab: index)
                    
                })
            }
            .padding(.top)
        .background(Color.clear)
        .frame(height: 100)
        .edgesIgnoringSafeArea(.all)
    }
}

struct TabBarItem: View {
    
    @Binding var currentTab: Int
    let namespace: Namespace.ID
    
    var tabBarItemName: String
    var tab: Int

    var body: some View {
        Button {
            self.currentTab = tab
        } label: {
            VStack {
                Spacer()
                Text(tabBarItemName)
                if currentTab == tab {
                    CustomColor.myColor
                        .frame(height: 2)
                        .matchedGeometryEffect(id: "underline",
                                               in: namespace,
                                               properties: .frame)
                } else {
                    Color.clear.frame(height: 2)
                }
            }
            .animation(.spring(), value: self.currentTab)
        }
        .fontWeight(.heavy)
        .buttonStyle(.plain)
    }
}

NavigationLink -> this is just the part of the code that contains the NavigationLink, this VStack of course is inside the NavigationView.

struct HomeView: View {
NavigationView {
...
     VStack(spacing: 15) {
        ForEach(self.data.datas.filter {(self.search.isEmpty ? true : $0.title.localizedCaseInsensitiveContains(self.search))}, id: \.id) { rs in
        NavigationLink(
           destination: ItemDetails(data: rs)
        ){
        RecentItemsView(data: rs)
                                        
         }
         .buttonStyle(PlainButtonStyle())
                                    
         }
      }
   }
}

ItemDetails

struct ItemDetails: View {
    
    let data: DataType
    var body : some View {
        NavigationView {
            VStack {
                AsyncImage(url: URL(string: data.pic), content: { image in
                    image.resizable()
                }, placeholder: {
                    ProgressView()
                })
                .aspectRatio(contentMode: .fill)
                .frame(width: 250, height: 250)
                .clipShape(RoundedRectangle(cornerRadius: 12.5))
                .padding(10)
                
                VStack(alignment: .leading, spacing: 8, content: {
                    
                    Text(data.title)
                        .fontWeight(.bold)
                        .frame(maxWidth: .infinity, alignment: .center)
                    Text(data.description)
                        .font(.caption)
                        .foregroundColor(.gray)
                        .frame(maxWidth: .infinity, alignment: .center)
                    
                })
                .padding(20)

            }
            .padding(.horizontal)
        }
            .navigationBarBackButtonHidden(true)
    }

}

I apologize for the garbage in the code, it seemed to me that there is not much of it and it does not interfere with understanding the code, also during the analysis of this problem on the Google\SO, I did not need to work with other parts of the code anywhere, except for those that I provided above, but if I missed something, then please let me know, thanks.

John Smith
  • 31
  • 1
  • 8
  • is the question `How to hide TabView in NavigationLink?` or `...hide the tabbar in NavigationLink` or the `TabBarView`? Also, in the `NavigationLink`, does it mean in the destination, like `ItemDetails` or what exactly. – workingdog support Ukraine Nov 09 '22 at 22:58
  • 1
    You should give apples best practices a look https://developer.apple.com/design/human-interface-guidelines/components/navigation-and-search/tab-bars/ – lorem ipsum Nov 09 '22 at 23:42
  • Does this answer your question? [SwiftUI hide TabBar in subview](https://stackoverflow.com/questions/58444689/swiftui-hide-tabbar-in-subview) –  Nov 10 '22 at 01:05
  • @workingdogsupportUkraine there is a `TabView` that displays three different views in one `tabbar`, one of which is a `HomeView` which has a `NavigationLink` (when displaying from the database, you can go to the item details by clicking on it from the list), but when I go to `ItemDetails`, then the `tabbar` with all buttons of other views staying on top, I need to hide this `tabbar` inside `ItemDetails` and also be able to go back to `HomeView`. Maybe I'm confusing some designations, but now I hope its more clearly. – John Smith Nov 10 '22 at 09:16
  • @MrDeveloper I tried it, I tried everything on **SO**.. the only thing that was more real was this: `https://thinkdiff.net/swiftui-how-to-pop-to-tabview-from-navigationview-fe760dee04d`, but there were problems with SceneDelegate... – John Smith Nov 10 '22 at 09:16

0 Answers0