Creating a left-arrow button (like UINavigationBar's "back" style) on a UIToolbar Creating a left-arrow button (like UINavigationBar's "back" style) on a UIToolbar ios ios

Creating a left-arrow button (like UINavigationBar's "back" style) on a UIToolbar


I used the following psd that I derived from http://www.teehanlax.com/blog/?p=447

http://www.chrisandtennille.com/pictures/backbutton.psd

I then just created a custom UIView that I use in the customView property of the toolbar item.

Works well for me.


Edit: As pointed out by PrairieHippo, maralbjo found that using the following, simple code did the trick (requires custom image in bundle) should be combined with this answer. So here is additional code:

// Creates a back button instead of default behaviour (displaying title of previous screen)UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back_arrow.png"]                                                               style:UIBarButtonItemStyleBordered                                                              target:self                                                              action:@selector(backAction)];tipsDetailViewController.navigationItem.leftBarButtonItem = backButton;[backButton release];


The Unicode Method

I think it is much easier to just use a unicode character to get the job done. You can look through arrows by googling either Unicode Triangles or Unicode Arrows. Starting with iOS6 Apple changed the character to be an emoji character with a border. To disable the border I add the 0xFE0E Unicode Variation Selector.

NSString *backArrowString = @"\U000025C0\U0000FE0E"; //BLACK LEFT-POINTING TRIANGLE PLUS VARIATION SELECTORUIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:backArrowString style:UIBarButtonItemStylePlain target:nil action:nil];self.navigationItem.leftButtonItem = backBarButtonItem;

The code block is just for the demo. It would work in any button that accepts an NSString.

For a full list of characters search Google for Unicode character and what you want. Here is the entry for Black Left-Pointing Triangle.

Result

The result


WARNING: There are reports that this will not work on iOS 6. This might only work on older versions of the OS. Evidently at least one dev has had their app rejected for using this trick (see the comments). Use at your own risk. Using an image (see answer above) might be a safer solution.

This can be done without adding in your own image files using sekr1t button type 101 to get the correct shape. For me the trick was figuring out that I could use initWithCustomView to create the BarButtonItem. I personally needed this for a dynamic navbar rather than a toolbar, but I tested it with a toolbar and the code is nearly the same:

// create buttonUIButton* backButton = [UIButton buttonWithType:101]; // left-pointing shape![backButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];[backButton setTitle:@"Back" forState:UIControlStateNormal];// create button item -- possible because UIButton subclasses UIView!UIBarButtonItem* backItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];// add to toolbar, or to a navbar (you should only have one of these!)[toolbar setItems:[NSArray arrayWithObject:backItem]];navItem.leftBarButtonItem = backItem;

If you're doing this on a toolbar you'll have to tweak how you set the items, but that depends on the rest of your code and I leave that as an exercise for the reader. :P This sample worked for me (compiled & run).

Blah blah, read the HIG, don't use undocumented features, and all that. There's only six supported button types and this isn't one of them.