ma11hew28
ma11hew28

Reputation: 126367

How to make UINavigationItem backBarButtonItem rectangular?

How can set self.navigationItem.backBarButtonItem of my RootViewController, so that the back button is rectangular instead of having a back arrow? I want to do this because I'm using a custom backBarButtonItem with an image of a grid of four squares (like the nine-square-gird image that the Facebook iPhone app uses for its home button).

Currently, in -[RootViewController initWitNibName:bundle:], I do:

self.navigationItem.backBarButtonItem =
[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"go-home.png"]
                                 style:UIBarButtonItemStylePlain
                                target:nil action:NULL];

Note: This does not cause a memory leak as I'm using ARC.

But, this makes the button have a left arrow. Is there a simple fix to make the button rectangular on all sides?

I know I could set the leftBarButtonItem for all of the view controllers that can get pushed from the RootViewController, but there are like five different options, so that'd be a lot of repetition. I guess I could make a method, e.g., +[Utils homeBarButtonItem], that creates the button above and then call self.navigationItem.leftBarButtonItem = [Utils homeBarButtonItem]; in each of the five view controllers' -viewDidLoad methods, but I'm wondering if there's a simple fix I'm missing.

Upvotes: 1

Views: 2566

Answers (1)

Matt Aitken
Matt Aitken

Reputation: 141

Sadly the only way, as you suggest, is to use a leftBarButtonItem and use a button builder utility class.

Set the action of your leftBarButtonItem to pop the view controller and you're done.

[self.navigationController popViewControllerAnimated:YES];

Upvotes: 3

Related Questions