Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI: Back button disappears when clicked on NavigationLink

Tags:

swiftui

I'm trying to add a NavigationLink at the top of the screen, but once I click it, it prompts me to the result and the Back button disappears.

SwiftUI Code:

NavigationView {
    VStack {
        NavigationLink (destination: Text("COOL")) {
            Text("COOL")
        }

        Spacer()
    }
    .navigationBarHidden(true)
    .navigationBarTitle(Text("Home"))
    //.edgesIgnoringSafeArea([.top, .bottom])
}

The back button disappears after clicking on the NavigationLink: https://gyazo.com/9d39936c849f570a05687e41096ddeca

like image 736
md123 Avatar asked Jan 04 '20 09:01

md123


People also ask

How do I change the back button icon in Swift?

If you interest in changing only a UINavigationBar back button in some UINavigationController , you can do that by set backIndicatorImage and backIndicatorTransitionMaskImage on a navigation bar instance. In our example, we set it in viewDidLoad() of a navigation controller's root view controller.

How do I add a navigation bar in SwiftUI?

To show a Navigation Bar using SwiftUI, we should use the NavigationView component that is responsible for this purpose. It requires that we provide the Content that is a View type. The Content can be anything from a text field to scrollable content. In short, it can be any SwiftUI view.

How do I change the navigation bar title in SwiftUI?

To customize a navigation bar title view in SwiftUI, we simply set ToolbarItem of placement type . principal to a new toolbar modifier. Text("Hello, SwiftUI!") <1> Because this is a customize of navigation bar title, a view needs to be embedded inside a NavigationView .


1 Answers

There is some glitch IMHO, when you use both .navigationBarHidden(true) and .navigationBarTitle(Text("Some text)). If you remove the last one, back button works as usual. Nevertheless I tried to return back button in your code snippet. It still has glitch while returning to first view, but back button don't disappear. I hope it will help and you will go further from here:

struct NotHiddenBackButton: View {

    @State var hiddingNavBar = true
    @State var goToSecondView = false

    var body: some View {

        NavigationView {

            NavigationLink(destination: ViewWithBackButton(hiddingNavBar: $hiddingNavBar), isActive: $goToSecondView) {

                VStack {
                    Text("COOL")
                        .onTapGesture {
                            self.hiddingNavBar = false
                            self.goToSecondView = true
                    }
                    Spacer()

                }


            }
            .navigationBarHidden(hiddingNavBar)
            .navigationBarTitle(Text("Home"))
        }


    }

}

struct ViewWithBackButton: View {

    @Binding var hiddingNavBar: Bool
    var body: some View {

        Text("Second view")
            .navigationBarTitle("Second view")
            .onDisappear() {
                self.hiddingNavBar = true
        }

    }

}
like image 68
Hrabovskyi Oleksandr Avatar answered Oct 01 '22 20:10

Hrabovskyi Oleksandr