Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UINavigationBar Hide back Button Text

People also ask

How do I make my back button invisible?

Way 1: Touch “Settings” -> “Display” -> “Navigation bar” -> “Buttons” -> “Button layout”. Choose the pattern in “Hide navigation bar” -> When the app opens, the navigation bar will be automatically hidden and you can swipe up from the bottom corner of the screen to show it.

How do I hide the navigation back button in Swift 4?

Go to attributes inspector and uncheck show Navigation Bar to hide back button.


In the interface builder, you can select the navigation item of the previous controller and change the Back Button string to what you'd like the back button to appear as. If you want it blank, for example, just put a space.

You can also change it with this line of code:

[self.navigationItem.backBarButtonItem setTitle:@"Title here"];

Or in Swift:

self.navigationItem.backBarButtonItem?.title = ""


You can implement UINavigationControllerDelegate like this:

Older Swift

func navigationController(navigationController: UINavigationController, willShowViewController viewController: UIViewController, animated: Bool) {
    let item = UIBarButtonItem(title: " ", style: .Plain, target: nil, action: nil)
    viewController.navigationItem.backBarButtonItem = item
}

Swift 4.x

class MyNavigationController: UINavigationController, UINavigationControllerDelegate {
    override func viewDidLoad() {
        super.viewDidLoad()
        self.delegate = self
    }

    func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
        let item = UIBarButtonItem(title: " ", style: .plain, target: nil, action: nil)
        viewController.navigationItem.backBarButtonItem = item
    }
}

backBarButtonItem is nil by default and it affects next pushed controller, so you just set it for all controllers


You could also do this through storyboard. In the attribute inspector of the navigation item of the previous controller you could set " " in the Back button field. Refer Image below. Replace "Your Title here" to " ". By doing this you will achieve the desired result. You don't need to mess with the 'Title' anymore.

Programmatically you could use

[self.navigationItem.backBarButtonItem setTitle:@" "];

where self refers to the controller which pushes your desired View controller.

Xcode view controller navigation item

Sample Before, After Navigation bar

Before Before

After After


Setting title of the back button to @"" or nil won't work. You need to set the entire button empty (without a title or image):

Objective-C

[self.navigationItem setBackBarButtonItem:[[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil]];

Swift

self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)

This should be done on the view controller that's on top of your view controller in navigation stack (i.e. from where you navigate to your VC via pushViewController method)


Another solution to this issue for situations where you have a great deal of view controllers is to use a UIAppearance proxy to effectively hide the back button title text like this:

UIBarButtonItem *navBarButtonAppearance = [UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil];

[navBarButtonAppearance setTitleTextAttributes:@{
    NSFontAttributeName:            [UIFont systemFontOfSize:0.1],
    NSForegroundColorAttributeName: [UIColor clearColor] }
                                      forState:UIControlStateNormal];

This solution will render the text as a tiny clear dot, similar to manually setting the back button title to @" ", except that it affects all nav bar buttons.

I don't suggest this as a general solution to the issue because it impacts all navigation bar buttons. It flips the paradigm so that you choose when to show the button titles, rather than when to hide the titles.

To choose when to show the titles, either manually restore the title text attributes as needed, or create a specialized subclass of UIBarButtonItem that does the same (potentially with another UIAppearance proxy).

If you have an app where most of the back button titles need to be hidden, and only a few (or none) of the nav buttons are system buttons with titles, this might be for you!

(Note: the font size change is needed even though the text color is clear in order to ensure that long titles do not cause the center nav bar title to shift over)


Add the following code in viewDidLoad or loadView

self.navigationController.navigationBar.topItem.title = @"";  

I tested it in iPhone and iPad with iOS 9