Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • backBarButtonItem
  • leftBarButtonItem

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

like image 769
Ayush Goel Avatar asked Aug 18 '15 15:08

Ayush Goel


People also ask

What is a uinavigationbar object?

A UINavigationBar object is a bar, typically displayed at the top of the window, containing buttons for navigating within a hierarchy of screens. The primary components are a left (back) button, a center title, and an optional right button.

How do I display the back button on a navigation item?

The backBarButtonItem property of a navigation item reflects the Back button you want to display when the current view controller is just below the topmost view controller. The Back button doesn’t appear when the current view controller is topmost. When specifying buttons for a navigation item, you must use UIBarButtonItem objects.

How do I use navigation items in uinavigationcontroller?

The managing UINavigationController object uses the navigation items of the topmost two view controllers to populate the navigation bar with content. A navigation item always reflects information about its associated view controller. The navigation item must provide a title to display when the view controller is topmost on the navigation stack.

Can I assign a custom view to the back bar button?

When configuring your bar button item, do not assign a custom view to it; the navigation item ignores custom views in the back bar button anyway. It’s very important to read the first sentence carefully, “When this navigation item is immediately below the top item in the stack… .”


2 Answers

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.

like image 191
Cole Avatar answered Oct 23 '22 11:10

Cole


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)
like image 21
Ayush Goel Avatar answered Oct 23 '22 10:10

Ayush Goel