3

how can I hide the TabBar when a new View is pushed via NavigationLink?

Here's how I push the next View:

TabView {
    NavigationView {
        List(fakeUser) { user in
            NavigationLink(destination: ChatDetailView(user: user)) {
                ChatCell(user: user)
            }
        }
        .navigationBarTitle("Chats")
        .navigationBarItems(leading: leadingBarItem, trailing: trailingBarItem)
    }
    .tabItem {
        Image(systemName: "message.fill")
            .font(.system(size: 20))
        Text("Chats")
    }
}
Blazej SLEBODA
  • 8,936
  • 7
  • 53
  • 93
SwiftiSwift
  • 7,528
  • 9
  • 56
  • 96
  • Well, I've managed to construct a view hierarchy, which gives a behavior which you requested, but on return to root SwfitUI throws exception `'NSInternalInconsistencyException', reason: 'Tried to pop to a view controller that doesn't exist.'`, which is complete nonsense from SwiftUI view construction approach. So, due to this I cannot propose it as answer, but theoretically it is possible, when SwiftUI becomes more stable. – Asperi Nov 23 '19 at 17:36
  • just post it and let the people know about that – SwiftiSwift Nov 24 '19 at 00:57
  • https://stackoverflow.com/questions/57304876/how-to-hide-the-tabbar-when-navigate-with-navigationlink-in-swiftui – zrfrank Mar 27 '20 at 05:42

2 Answers2

2

Caution: rise exception on Xcode 11.2/iOS 13.2

Here is a relayout which gives an effect you requested, as far as I understood.

However, although there is nothing criminal in below code, on navigate back internals of UIKit got into exception:

 2019-11-24 10:54:36.644037+0200 Test[1180:41920] *** Terminating
app due to  uncaught exception 'NSInternalInconsistencyException',
reason:  'Tried to pop to a view controller that doesn't exist.'

*** First throw call stack: (     0   CoreFoundation                      0x00007fff23c4f02e __exceptionPreprocess + 350  1   libobjc.A.dylib   
0x00007fff50b97b20 objc_exception_throw + 48  2   CoreFoundation      
0x00007fff23c4eda8 +[NSException raise:format:arguments:] + 88    3  
Foundation                          0x00007fff256c9b61
-[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 191     4  
UIKitCore                           0x00007fff4713d9d1
__57-[UINavigationController popToViewController:transition:]_block_invoke + 620 

Approach code

var body: some View {
    NavigationView {
        TabView {
            List(fakeUser) { user in
                NavigationLink(destination: ChatDetailView(user: user)) {
                    ChatCell(user: user)
                }
            }
            .navigationBarTitle("Chats")
            .navigationBarItems(leading: leadingBarItem, trailing: trailingBarItem)
            .tabItem {
                Image(systemName: "message.fill")
                    .font(.system(size: 20))
                Text("Chats")
            }
        }
        .navigationBarTitle("Chats")
    }
}
Asperi
  • 228,894
  • 20
  • 464
  • 690
1

In iOS 16 you have this option:

.toolbar(.hidden, for: .tabBar)

Just attach it to the view which is going to be pushed.