Ayush Goel
Ayush Goel

Reputation: 3154

Reference the back button on UINavigationBar

I need a reference to the back button in the UINavigationBar or UINavigationItem. The Back button in the image below.

Navigation Bar

I am not customizing the button, thus

navigationItem.backBarButtonItem
navigationItem.leftBarButtonItem

these both are nil.

Is there any way to get a reference to the button without customizing it?

Reference:

Update: My comment might help to understand why I don't want to customize the back button. :)

Upvotes: 5

Views: 2363

Answers (4)

Vishnuvardhan
Vishnuvardhan

Reputation: 5107

Try this it might help you.

 UINavigationItem *i = [self.navigationController.navigationBar.items firstObject];
    i.title = @"Hai";

Upvotes: 1

Mudith Chathuranga Silva
Mudith Chathuranga Silva

Reputation: 7444

try out this one

let backItem = UIBarButtonItem(title: "Custom Text HERE", style: .Bordered, target: nil, action: nil)
navigationItem.backBarButtonItem = backItem

Upvotes: 1

Ayush Goel
Ayush Goel

Reputation: 3154

In reference to my use case - Ayush's comment

Till the time I get a concrete solution, I have created a dummy view below the navigation bar (below the back button) to show the tip from. Here is the code.

let v = UIView(frame: CGRectMake(0, 0, 50, 0))
view.addSubview(v)
let tipText = "Here it the back button"
EasyTipView.showAnimated(true,
  forView: v,
  withinSuperview: view,
  text:tipText,
  preferences: nil,
  delegate: self)

Upvotes: 1

Cole
Cole

Reputation: 2646

The Documentation says this about the backBarButtonItem:

When this item is the back item of the navigation bar—when it is the next item below the top item—it may be represented as a back button on the navigation bar. Use this property to specify the back button. The default value is a button displaying the navigation item’s title.

So the backBarButtonItem is always nil by default because it belongs to the previous view controller. The only way to get a non-nil reference is by customizing it.

If you just want to change the name, however, that can be done in the previous View Controller.

Upvotes: 6

Related Questions