4

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.
tc.
  • 33,468
  • 5
  • 78
  • 96
Gargo
  • 1,135
  • 1
  • 10
  • 21

4 Answers4

13

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!

karse23
  • 4,055
  • 6
  • 29
  • 33
  • thanks, I will use this if there are wouldn't any other way. But I hoped to make it more flexible (when it depends on an image and a style of the backBarButtonItem only and doesn't depend on the image resources) – Gargo Sep 13 '11 at 10:11
  • Your second example is much worse because sdk doesn't allow to set an image and a title for this button at once. So I should make different images for all the labels. Is that you want to advice me? – Gargo Sep 14 '11 at 11:33
  • Well... I have not looked all styles and types of buttons that navigationbars can have... could you try to change the UIBarButtonItemStyleBordered for another style? maybe there is some style that is like the go back button... and then you could start with a initWithTitle instead the initWithImage. I cannot make more samples because I don't have a Mac now, later i'll try it! – karse23 Sep 14 '11 at 12:02
  • You can use a virtual machine and/or "Hack OS" – Gargo Sep 15 '11 at 06:49
  • I meant that I only have a mac to try it when I'm at job :) – karse23 Sep 16 '11 at 09:44
  • if you use [UIButton buttonWithType:101]; you get the backButton style, however it is an undocumented feature. – Daniel Oct 06 '11 at 08:52
  • Also, here is a good XCF image of the back button: http://elucidatedbinary.com/blog/2012/10/16/ios-back-button-image/ – Morkrom May 03 '13 at 14:18
  • This is a very nice and workable solution: http://www.hkwebentrepreneurs.com/2013/11/ios-prevent-back-button-navigating-to.html – Sharique Abdullah Jan 26 '16 at 17:53
0

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

Jaffa
  • 12,442
  • 4
  • 49
  • 101
0

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.

aslı
  • 8,740
  • 10
  • 59
  • 80
  • As I have understood you I need to change the backBarButtonItem title, then I need to convert this one to UIView and assign it to a leftBarButtonItem. But how? – Gargo Sep 13 '11 at 09:07
  • What I would do is to create a simple UIButton, customize this the way I want it to look like, I mean give it a background image, a title, etc.. You can find an image that's similar to the backBarButtonItem and assign this to your newly created UIButton. And then assign a target action to this UIButton so that it does what you'd like to do when it's tapped. And finally, assign this UIButton as the leftBarButton of your nav. controller as described above. – aslı Sep 13 '11 at 09:48
  • Now I think It would be better to delete the leftBarButtonItem and put its function into viewWillDisapear – Gargo Sep 14 '11 at 06:16
  • ok then. at least now you know about the other alternatives :) – aslı Sep 14 '11 at 20:09
0

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];
willy
  • 490
  • 4
  • 11
  • I never had a rejection because of this – willy Sep 14 '11 at 11:35
  • It looks like this solution uses a "custom" UINavigationBar (`_navigationBar = [[UINavigationBar alloc] initWithFrame:...]; [self.view addSubview:_navigationBar]; _navigationBar.items = ...; _navigationBar.delegate = self`) instead of `self.navigationController.navigationBar`. You'll need to implement UINavigatonBarDelegate appropriately. – tc. Sep 14 '11 at 19:56