Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUi Navigation Bar Button disappears after entering the third View (Controller)

I have a huge Problem with SwiftUi. The "Backbutton" in a really Simple NavigationView Hierarchy disappears on the third View. If I go one view further, the Backbutton is there again and I can go back.

I searched like 3 Hours but only found this SwiftUI: Back button disappears when clicked on NavigationLink

Obviously this doesn't solve my Problem.

Thanks for any Help!

like image 713
Pitchbloas Avatar asked Oct 18 '20 02:10

Pitchbloas


3 Answers

I found another workaround for who doesn't want to use a deprecated method.

Just add in your .toolbar this ToolBarItem:

.toolbar {

    // ... other toolbar items

    ToolbarItem(placement: .navigationBarLeading) {
        Text("")
    }
}
like image 88
Lorenzo Fiamingo Avatar answered Oct 16 '22 06:10

Lorenzo Fiamingo


Another solution which seems to be working on Xcode 12.4:

ToolbarItem(placement: .navigationBarLeading) {
    HStack {}
}
like image 38
titusmagnus Avatar answered Oct 16 '22 05:10

titusmagnus


In case you want to make your code cleaner

/// A ToolbarItem wrapper to work around the back button disappearance bug in SwiftUI 2.
struct NavbarBackButtonDisappearanceWorkaroundItem: ToolbarContent {
    var body: some ToolbarContent {
        ToolbarItem(placement: .navigationBarLeading) {
            Color.clear
        }
    }
}

Use as follows:

.toolbar {
   NavbarBackButtonDisappearanceWorkaroundItem()
   SomeOtherUsefulItem()        
}

like image 39
Shengchalover Avatar answered Oct 16 '22 06:10

Shengchalover