1

E.g. if you navigate from View A to View B, View B will typically display a back button on the left side of the navigation bar that says "< View A" (assuming that View A's title is "View A").

When I don't have a title in View A, the back button in view B is simply "<" which is as expected, because view A has no title.

Now is there a way to make it so that if there is no title in the previous view, it should always say "< Back"?

enter image description here

pizzae
  • 2,815
  • 6
  • 26
  • 45

1 Answers1

1

You can make your own back button which can be individualized:

Put this at the top of your view:

@Environment(\.presentationMode) var presentationMode

var btnBack : some View { Button(action: {
    self.presentationMode.wrappedValue.dismiss()
}) {
    Text("< Back")
    }
}

And at at the end of your Stack in that View:

.navigationBarBackButtonHidden(true)    
.navigationBarItems(leading: btnBack)
Kuhlemann
  • 3,066
  • 3
  • 14
  • 41
  • Works perfectly! Is there a way to make it like native, i.e. the "<" uses the actual back icon instead? – pizzae May 15 '20 at 12:46
  • Use Image(systemName: "chevron.left") before Text(" Back"). – Kuhlemann May 15 '20 at 13:06
  • @pizzae, as far as I remember this approach disables swipe-to-back, just in case. – Asperi May 15 '20 at 13:26
  • @Asperi Yep I just encountered that problem and managed to solve it with the code from this answer: https://stackoverflow.com/a/60067845/6280334 – pizzae May 15 '20 at 13:38