lm / navigation-stack-backport

SwiftUI NavigationStack for iOS 14 and 15
MIT License
106 stars 17 forks source link

.navigationBarHidden(true) seems not work for iOS 15 #4

Open Horse888 opened 9 months ago

Horse888 commented 9 months ago
                .navigationBarTitleDisplayMode(.inline)
                .navigationTitle("")
                .navigationBarHidden(true)

The navigationBar is there, not be hidden.

lm commented 9 months ago

Hello, please provide full reproducible SwiftUI view code or project and specify exact iOS version where the problem occurs.

Horse888 commented 9 months ago

Sorry, just find that this bug will not be related to this lib.

The code as bellow:

            NavigationStackBackport.NavigationStack(path: $lifeModel.wordNaviPath) {
                TabView(selection: $lifeModel.tabTag) {
                    WordsPagingView()
.navigationBarTitleDisplayMode(.inline)
                .navigationTitle("")
                .navigationBarHidden(true)
...

This issue occurred when a TabView be embedded in a NavigationView.

Even try StackOverflow with no luck.

Horse888 commented 9 months ago

@lm Hi lm, here is the demo code.

import SwiftUI
import NavigationStackBackport

enum TestTab {
    case test1
    case test2
    case test3
}

struct ContentView: View {
    @State var selectedTab: TestTab = .test1

    var body: some View {
        ZStack {
            NavigationStackBackport.NavigationStack { // Can't hide the navigation bar
//          NavigationView { // Using NavigationView is ok.
                TabView(selection: $selectedTab) {
                    // Tab A
                    List(1...1000, id: \.self) { i in
                        Text("Item A: \(i)")
                    }
                    .navigationBarHidden(true).navigationBarTitle("a")

                    .tag(TestTab.test1)
                    .tabItem { Label("Home", systemImage: "n.circle") }

                    // Tab B
                    List(1...1000, id: \.self) { i in
                        Text("Item B: \(i)")
                    }
                    .navigationBarHidden(true).navigationBarTitle("b")
                    .tag(TestTab.test2)
                    .tabItem { Label("Stories", systemImage: "book") }

                    // Tab C
                    List(1...1000, id: \.self) { i in
                        Text("Item C: \(i)")
                    }
                    .navigationBarHidden(true).navigationBarTitle("c")
                    .tag(TestTab.test3)
                    .tabItem { Label("Settings", systemImage: "gear") }
                }
            }
            .navigationViewStyle(StackNavigationViewStyle())
        }
    }
}

#Preview {
    ContentView()
}

When I useNavigationView instead of NavigationStackBackport.NavigationStack, the navigationBar can be hidden.

Horse888 commented 9 months ago

Demo device is an iOS 15 simulator.

lm commented 9 months ago

Usually each tab has it's own navigation stack. Could that work for you? it would probably fix issues like this.

Horse888 commented 9 months ago

Yes, usually it is. Current my requirement: I need to hide the tabBar when push to detail page. I add something like .toolbar(.hidden, for: .tabBar), the tabBar can be hidden, and can be shown when pop back.

BUT The only issue is the tabBar will appear immediately(with no animation) when pop back. I want it's smooth appear or keep there.

So I embed the TabView in the NavigationView.

abcdeiko commented 7 months ago

Horse888 Yes, there is a problem with hiding navigation bar on ios < 16. To do that you need to add navigationbarhidden code as the same level as your main view inside NavigationStackView is.

This code will show navigation bar when you pop from SomePushableView to SomeView:

NavigationStack(path: someVarPath) {
            SomeView()
            .navBackport
            .navigationDestination(for: SomeNavigationItem.self) {
                SomePushableView().navigationBarHidden(true)
            }
        }

This code won't show navigation bar when you pop from SomePushableView to SomeView:

NavigationStack(path: someVarPath) {
            SomeView()
            .navBackport
            .navigationDestination(for: SomeNavigationItem.self) {
                SomePushableView().navigationBarHidden(true)
            }
            .navigationBarHidden(true)
        }