Gargo
Gargo

Reputation: 1380

How to make leftBarButtonItem looking like backBarButtonItem?

The default solvings are not appropriate:

  1. to change the previous ViewController title - I need to make my own function controlling the button touches up
  2. to make a leftBarButtonItem and hide backBarButtonItem - leftBarButtonItem doesn't look like a default backBarButtonItem.

Upvotes: 4

Views: 16258

Answers (4)

willy
willy

Reputation: 490

Here is a hack to add a back-like button:

UINavigationItem* backNavigationItem = [[UINavigationItem alloc] initWithTitle:@"Back"];
[_navigationBar pushNavigationItem:backNavigationItem animated:NO];
[backNavigationItem release];

You can add more items (such like title, right button) this way:

UINavigationItem *navigationItem = [[UINavigationItem alloc] initWithTitle:@"Title"];
navigationItem.rightBarButtonItem = // Some UIBarButtonItem
[_navigationBar pushNavigationItem:navigationItem animated:NO];
[navigationItem release];

Upvotes: 0

karse23
karse23

Reputation: 4115

Here's an example to create a custom leftBarButtonItem:

UIImage *buttonImage = [UIImage imageNamed:@"backbutton.png"];
UIButton *aButton = [UIbutton buttonWithType:UIButtonTypeCustom];
[aButton setImage:buttonImage forState:UIControlStateNormal];
aButton.frame = CGRectMake(0.0,0.0,buttonImage.size.width,buttonImage.size.height);
[aButton addTarget:self action:@selector(aSelector) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:aButton];
self.navigationItem.leftButtonItem = backButton;

Hope it helps...

Edit:

Try this...

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"yourimage.jpg"] style:UIBarButtonItemStyleBordered target:self action:@selector(aSelector)];
self.navigationItem.leftBarButtonItem = backButton;

I don't remember what type of button is the backbutton, try to change the default style to other. Good luck!

Upvotes: 13

aslı
aslı

Reputation: 8914

You can assign your custom views as the bar buttons to your navigation controller, like this:

UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithCustomView:yourCustomizedControl];
[yourCustomizedControl release];

self.navigationItem.leftBarButtonItem = customItem;
[customItem release];

Check this Apple documentation for a more detailed explanation, if you want.

Upvotes: 0

Jaffa
Jaffa

Reputation: 12710

Just create a custom barbuttonitem and customize it with a picture similar to the back button item. You can get its text from the view controllers in the navigation stack.

Then handle the taps to implement the desired effect

Upvotes: 0

Related Questions