10

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!

  • Does this answer your question? [Toolbar is deleting my back Button in the NavigationView](https://stackoverflow.com/questions/64405106/toolbar-is-deleting-my-back-button-in-the-navigationview) – pawello2222 Oct 18 '20 at 08:56

7 Answers7

10

For me the issue was a bit different - the back button disappeared on the third view only after interacting with the third view, e.g. clicking into the list view.

My workaround is to use the old .navigationBarItems instead of .toolbar, so:

 .navigationBarItems(trailing:
    Menu {
       Button(action: {
          //some action
       }) {
          //some label
       }
       Button(action: {
          //some action
       }) {
          //some label
       }                                        
    }
    label: {
       //some label
    }
 )

instead of:

.toolbar {
   ToolbarItem(placement: .navigationBarTrailing) {
      Menu {
         Button(action: {
            //some action
         }) {
            //some label
         }
         Button(action: {
            //some action
         }) {
            //some label
         }                        
      }
      label: {
         //some label
      }
   }
}
user14418533
  • 101
  • 3
  • 3
    I'm encountering the same bug too. But while using navigationBarItems does fix the issue, this method is deprecated. The bug also affects any attempt to use the bottom toolbar as well. – Brandon C. Nov 24 '20 at 00:14
9

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("")
    }
}
Lorenzo Fiamingo
  • 3,251
  • 2
  • 17
  • 35
6

Another solution which seems to be working on Xcode 12.4:

ToolbarItem(placement: .navigationBarLeading) {
    HStack {}
}
titusmagnus
  • 2,014
  • 3
  • 23
  • 23
  • Just adding this above my ToolbarItem(placement: .navigationBarTrailing) worked like a charm everywhere. Also still works in Xcode 13+ – Dru Freeman Dec 08 '21 at 17:36
5

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

Shengchalover
  • 614
  • 5
  • 10
4

I found the Problem!

The .toolbar modifier on the NavigationView hides the Backbutton in a Buggy way!

  • 3
    I feel like this doesn't actually explain much. Did you discover any more info as to what the "buggy way" is, or why it might be behaving that way? – Aaron Krauss Jun 23 '21 at 20:10
  • I don't think it's just that, even the slide-back gesture doesn't work when the bug occurs to me. – ThiagoAM Sep 30 '21 at 06:14
1

I have found the solution of this problem. If you are not using .toolbar nor .navigationBarItems you should simply add .navigationBarItems(trailing: EmptyView()) to you children. This will always keep your back button alive)

   var body: some View {
      ScrollView() {
        ...
      }
       .navigationBarItems(trailing: EmptyView())
     }

'''

Matviy Suk
  • 11
  • 1
0

you need to add to your NavigationView

.navigationViewStyle(StackNavigationViewStyle())
rsergiu2003
  • 102
  • 3