How to Hide the Tabbar When Navigate with Navigationlink in Swiftui

SwiftUI Hide TabView bar inside NavigationLink views

If we talk about standard TabView, the possible workaround solution can be based on TabBarAccessor from my answer on Programmatically detect Tab Bar or TabView height in SwiftUI

Here is a required modification in tab item holding NavigationView. Tested with Xcode 11.4 / iOS 13.4

demo

struct FirstTabView: View {
@State private var tabBar: UITabBar! = nil

var body: some View {
NavigationView {
NavigationLink(destination:
FirstChildView()
.onAppear { self.tabBar.isHidden = true } // !!
.onDisappear { self.tabBar.isHidden = false } // !!
) {
Text("Go to...")
}
.navigationBarTitle("FirstTitle", displayMode: .inline)
}
.background(TabBarAccessor { tabbar in // << here !!
self.tabBar = tabbar
})
}
}

Note: or course if FirstTabView should be reusable and can be instantiated standalone, then tabBar property inside should be made optional and handle ansbsent tabBar explicitly.

hide TabView after clicking on a NavigationLink in SwiftUI

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.

How can I hide TabBar Swift UI?

Create CustumPresentViewController.swift -

    import UIKit
import SwiftUI

struct ViewControllerHolder {
weak var value: UIViewController?
}

struct ViewControllerKey: EnvironmentKey {
static var defaultValue: ViewControllerHolder { return
ViewControllerHolder(value:
UIApplication.shared.windows.first?.rootViewController ) }
}

extension EnvironmentValues {
var viewController: ViewControllerHolder {
get { return self[ViewControllerKey.self] }
set { self[ViewControllerKey.self] = newValue }
}
}

extension UIViewController {
func present<Content: View>(style: UIModalPresentationStyle =
.automatic, @ViewBuilder builder: () -> Content) {
// Must instantiate HostingController with some sort of view...
let toPresent = UIHostingController(rootView:
AnyView(EmptyView()))
toPresent.modalPresentationStyle = style
// ... but then we can reset rootView to include the environment
toPresent.rootView = AnyView(
builder()
.environment(\.viewController, ViewControllerHolder(value:
toPresent))
)
self.present(toPresent, animated: true, completion: nil)
}
}

Use this in required View -

@Environment(\.viewController) private var viewControllerHolder:  
ViewControllerHolder

private var viewController: UIViewController? {
self.viewControllerHolder.value
}

var body: some View {
NavigationView {
ZStack {
Text("Navigate")
}.onTapGesture {
self.viewController?.present(style: .fullScreen) {
EditUserView()
}
}
}
}

Hiding tab bar on a specific page in SwiftUI

I updated my solution with TabView for your situation. The same idea: you're using ZStack and @State var selection. And the idea is to use .opacity of TabView and YourCameraView (which is just Image(systemName: "plus.circle") in my example):

struct TabViewModel: View {

@State var selection: Int = 0

var body: some View {

ZStack {
GeometryReader { geometry in
TabView(selection: self.$selection) {

Text("list")
.tabItem {
Image(systemName: "list.bullet.below.rectangle")
}.tag(0)

Text("plus")
.tabItem {
Image(systemName: "camera")
}.tag(1)

Text("more categories!")
.tabItem {
Image(systemName: "square.grid.2x2")
}.tag(2)
}
.opacity(self.selection == 1 ? 0.01 : 1)

Image(systemName: "plus.circle")
.resizable()
.frame(width: 60, height: 60)
.shadow(color: .gray, radius: 2, x: 0, y: 5)
.offset(x: geometry.size.width / 2 - 30, y: geometry.size.height - 80)
.onTapGesture {
self.selection = 0
}
.opacity(self.selection == 1 ? 1 : 0)
}

}

}
}

when you tap on camera tabItem TabView becomes invisible

Hide TabBar when a new view is pushed in SwiftUI

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")
}
}


Related Topics



Leave a reply



Submit