tech_human
tech_human

Reputation: 7110

SwiftUI - Hiding navigation bar during certain conditions

Is it possible to show/hide navigation bar on the view while the view is open, but during certain conditions I wish to show navigation bar and during other conditions I wish to hide it.

Let's say when I open the view, I wish to show navigation bar with title, but when an event gets canceled, I display a message on the view and during this point I wish to hide the nav bar. For this I am creating a state var "hideNavBar" which I initially set to false and then when an event is canceled i.e. view model's state changes to 'eventCanceled', in my view I am updating state variable's value to true. When "hideNavBar" is set to true, I see navigation bar hiding from the view, but when I set it to false again in onDisappear() I don't see navigation bar.

My entire app has one Navigation View which is in my app file. ContentView() in my app file opens all the other views, so entire app is using same navigation view.

Is it possible to show and hide navigation bar when the view is open?

Below is the code:

class CustomViewModel: ObservableObject {
    enum EventDetailsStates {
        case idle
        case loading
        case eventCanceled
    }
    @Published var state = EventDetailsStates.idle
    @Published var reloadData = false
    
    init() {
        NotificationCenter.default.addObserver(forName: Notification.Name("eventCanceled"), object: nil, queue: nil, using: self.processEventCanceledNotification(_:))
    }
    
    @objc func processEventCanceledNotification(_ notification: Notification) {
        // Do something
        self.reloadData = false
        self.state = .eventCanceled
     }
}

struct CustomView: View {
    @ObservedObject var viewModel: CustomViewModel
    @State var showMessageView = false
    @State var hideNavBar = false
    @Environment(\.presentationMode) var presentationMode

    var body: some View {
        VStack {
            switch viewModel.state {
            // Some other states.

            case .eventCanceled:
            if self.showMessageView {
                MessageView().onAppear() {
                    self.hideNavBar = true
                }.onDisappear() {
                    self.hideNavBar = false
                    presentationMode.wrappedValue.dismiss()
                }
            }
            }
        }.navigationBarBackButtonHidden(hideNavBar).navigationBarHidden(hideNavBar).navigationBarTitle("Back")
        .onAppear() {
            if viewModel.reloadData {
                viewModel.state = .idle
            } else {
                self.showMessageView = true
                
                DispatchQueue.main.asyncAfter(deadline: .now() + 15.0) {
                    self.showMessageView = false
                }
            }
        }
    }
}

Adding app file's code:

import SwiftUI

@main
struct watchApp: App {
    @WKExtensionDelegateAdaptor(ExtensionDelegate.self) var delegate
    @SceneBuilder var body: some Scene {
        WindowGroup {
            NavigationView {
                ContentView().environmentObject(UserSettingsStore())
            }
        }

        WKNotificationScene(controller: NotificationController.self, category: "myCategory")
    }
}

Upvotes: 3

Views: 839

Answers (1)

Steven-Carrot
Steven-Carrot

Reputation: 3071

You are supposed to put value of navigationBarBackButtonHidden() as true if you want to hide it, but in all your steps, the value is always false which mean Hidden = false.

Upvotes: 1

Related Questions