Note that there are some explanatory texts on larger screens.

plurals
  1. POiOS Custom Back Button without calling method in every View Controller
    text
    copied!<p>I would like a more elegant solution to changing the back button for an Application's Navigation Bar. All the answers I've seen only talk about customizing a button and then adding at least one line of code to each View Controller for the new button to display.</p> <p>I have a category where I modify the Navigation Bar's background and Title. It's a category of <code>UINavigationBar</code>. The category calls <code>willMoveToWindow:</code> to call methods each time a new view is pushed on the Navigation Controller.</p> <p>So here's how the code to create a new button would look:</p> <pre><code>- (void)applyCustomButton{ UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 26, 26)]; [backButton setImage:[UIImage imageNamed:@"arrow-left"] forState:UIControlStateNormal]; [backButton setShowsTouchWhenHighlighted:TRUE]; [backButton addTarget:self action:@selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchDown]; UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton]; [self.navigationItem setLeftBarButtonItem:barButtonItem]; } </code></pre> <p>The problem here is that <code>self.navigationItem</code> won't work here. I don't want to bore you with all the things I tried categorizing, but that property only works on View Controllers (I think)?</p> <p>You don't have to give me a solution for the code above, maybe you have a better way of customizing the Back Button on the Navigation Bar without having to write code on every View Controller Class I have. I just hate repetition because maintaining code like that sucks.</p> <p>Thanks in advance. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload