16

I've recently started working in SwiftUI, came to the conclusion that working with navigation isn't really great yet. What I'm trying to achieve is the following. I finally managed to get rid of the translucent background without making the application crash, but now I ran into the next issue. How can I get rid of the "back" text inside the navbaritem?

enter image description here

I achieved the view above by setting the default appearance in the SceneDelegate.swift file like this.

let newNavAppearance = UINavigationBarAppearance()
newNavAppearance.configureWithTransparentBackground()
newNavAppearance.setBackIndicatorImage(UIImage(named: "backButton"), transitionMaskImage: UIImage(named: "backButton"))
newNavAppearance.titleTextAttributes = [
    .font: UIFont(name: GTWalsheim.bold.name, size: 18)!,
    .backgroundColor: UIColor.white

]

UINavigationBar.appearance().standardAppearance = newNavAppearance

One possible way that I could achieve this is by overriding the navigation bar items, however this has one downside (SwiftUI Custom Back Button Text for NavigationView) as the creator of this issue already said, the back gesture stops working after you override the navigation bar items. With that I'm also wondering how I could set the foregroundColor of the back button. It now has the default blue color, however I'd like to overwrite this with another color.

Jordy
  • 844
  • 2
  • 8
  • 27

8 Answers8

21

It's actually really easy. The following solution is the fastest and cleanest i made.

Put this at the bottom of your SceneDelegate for example.

extension UINavigationController {
    // Remove back button text 
    open override func viewWillLayoutSubviews() {
        navigationBar.topItem?.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
    }
}

This will remove the back button text from every NavigationView (UINavigationController) in your app.

Pitchbloas
  • 439
  • 5
  • 12
21

Piggy-backing on the solution @Pitchbloas offered, this method just involves setting the backButtonDisplayMode property to .minimal:

extension UINavigationController {

  open override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()
    navigationBar.topItem?.backButtonDisplayMode = .minimal
  }

}
blau
  • 1,063
  • 11
  • 18
14

I have found a straightforward approach to remove the back button text using SwiftUI only, and keeping the original chevron.

A drag gesture is added to mimic the classic navigation back button when user wants to go back by swiping right.

import SwiftUI

struct ContentView: View {

  @Environment(\.presentationMode) var presentation

  var body: some View {
    ZStack {
      // Your main view code here with a ZStack to have the
      // gesture on all the view.
    }
    .navigationBarBackButtonHidden(true)
    .navigationBarItems(
      leading: Button(action: { presentation.wrappedValue.dismiss() }) {
        Image(systemName: "chevron.left")
          .foregroundColor(.blue)
          .imageScale(.large) })
    .contentShape(Rectangle()) // Start of the gesture to dismiss the navigation
    .gesture(
      DragGesture(coordinateSpace: .local)
        .onEnded { value in
          if value.translation.width > .zero
              && value.translation.height > -30
              && value.translation.height < 30 {
            presentation.wrappedValue.dismiss()
          }
        }
    )
  }
}
Roland Lariotte
  • 1,987
  • 1
  • 10
  • 33
  • This is awesome! I will mention that presentation.wrappedValue.dismiss has been a headache in a project, sometimes causing app crashes in specific instances. – Alex Hartford Jan 27 '22 at 18:44
2

Standard Back button title is taken from navigation bar title of previous screen.

It is possible the following approach to get needed effect:

demo

struct TestBackButtonTitle: View {
    @State private var hasTitle = true
    var body: some View {
        NavigationView {
            NavigationLink("Go", destination:
                Text("Details")
                    .onAppear {
                        self.hasTitle = false
                    }
                    .onDisappear {
                        self.hasTitle = true
                    }
            )
            .navigationBarTitle(self.hasTitle ? "Master" : "")
        }
    }
}
Asperi
  • 173,274
  • 14
  • 284
  • 455
  • 5
    Alright yes this is a possible solution, but it's really nasty, if I'd have to do that on EVERY screen it would be kind of annoying and hacky. I'm looking for a way to globally disable this text. – Jordy Mar 04 '20 at 14:51
2

So I actually ended up with the following solution that actually works. I am overwriting the navigation bar items like so

.navigationBarItems(leading:
    Image("backButton")
        .foregroundColor(.blue)
        .onTapGesture {
            self.presentationMode.wrappedValue.dismiss()
    }
)

The only issue with this was that the back gesture wasn't working so that was solved by actually extending the UINavigationController

extension UINavigationController: UIGestureRecognizerDelegate {
    override open func viewDidLoad() {
        super.viewDidLoad()
        interactivePopGestureRecognizer?.delegate = self
    }

    public func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        return viewControllers.count > 1
    }
}

Now it's looking exactly the way I want it, the solution is kinda hacky... but it works for now, hopefully SwiftUI will mature a little bit so this can be done easier.

Jordy
  • 844
  • 2
  • 8
  • 27
  • Does't this give you duplicate navigation and back button on swipe back ? for me it was the case. So I ended up increasing navigation title width using toolbar. – Prafulla Apr 16 '21 at 05:42
1

Using the Introspect framework, you can easily gain access to the underlying navigation item and set the backButtonDisplayMode to minimal.

Here’s how you might use that in the view that was pushed

var body: some View {
    Text("Your body here")
        .introspectNavigationController { navController in
            navController.navigationBar.topItem?.backButtonDisplayMode = .minimal
        }
}
Aaron T
  • 129
  • 1
  • 5
0

custom navigationBarItems and self.presentationMode.wrappedValue.dismiss() worked but you are not allow to perform swiping back

You can either add the following code to make the swipe back again

//perform gesture go back
extension UINavigationController: UIGestureRecognizerDelegate {
    override open func viewDidLoad() {
        super.viewDidLoad()
        interactivePopGestureRecognizer?.delegate = self
    }

    public func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        return viewControllers.count > 1
    }
}

but the problem is, sometimes it will make your app crashed when you swipe half the screen and then cancel.

I would suggest the other way to remove the "Back" text. Adding the isActive state to monitor whether the current screen is active or not. :)

struct ContentView: View {
    @State var isActive = false

    var body: some View {
        NavigationView() {
            NavigationLink(
                "Next",
                destination: Text("Second Page").navigationBarTitle("Second"),
                isActive: $isActive
            )
                .navigationBarTitle(!isActive ? "Title" : "", displayMode: .inline)
        }
    }
}
  • On iOS 15.2 this (the second fix using the $isActive binding) works to disable the back button text but it causes a random item to be selected on navigation and the debug console shows the error "SwiftUI encountered an issue when pushing aNavigationLink. Please file a bug." – tdl Dec 31 '21 at 22:40
0

I am accomplishing this by changing the title of the master screen before pushing the detail screen and then setting it back when it re-appears. The only caveat is when you go back to the master screen the title's re-appearance is a little noticeable.

Summary:

  • on master view add state var (e.g. isDetailShowing) to store if detail screen is showing or not

  • on master view use the navigationTitle modifier to set the title based on the current value of isDetailShowing

  • on master view use onAppear modifier to set the value of isDetailShowing to false

  • on the NavigationLink in master screen use the simultaneousGesture modifier to set the isDetailShowing to true

    struct MasterView: View {
      @State var isDetailShowing = false
    
      var body: some View {
    
          VStack {
              Spacer()
                  .frame(height: 20)
              Text("Master Screen")
                  .frame(maxWidth: .infinity, alignment: .leading)
              Spacer()
                  .frame(height: 20)
    
              NavigationLink(destination: DetailView()) {
                  Text("Go to detail screen")
              }
              .simultaneousGesture(TapGesture().onEnded() {
                  isDetailShowing = true
              })
          }
          .navigationBarTitleDisplayMode(.inline)
          .navigationTitle(isDetailShowing ? "" : "Master Screen Title")
          .onAppear() {
              isDetailShowing = false
          }
      }
    }
    
    struct DetailView: View {
      var body: some View {
          Text("This is the detail screen")
          .navigationBarTitleDisplayMode(.inline)
          .navigationTitle("Detail Screen Title")
      }
    }
    
cigien
  • 55,661
  • 11
  • 60
  • 99
DCDC
  • 396
  • 4
  • 8