1

it's possible insert a toolbutton in a tool bar with the shape like the back button used in the navigation bar? here is what I mean:

sample

thanks in advance!

ghiboz
  • 7,863
  • 21
  • 85
  • 131
  • I'm not sure I understand what you are trying to achieve. Are you trying to combine a toolbar and a navigation bar functionally into a single bar? – MiguelB Jul 12 '11 at 13:34
  • no sorry, in my view I have only a toolbar, but I need to create a button like the back, but the back with the arrow shape is only active if I use a navigation bar, but I have only a tool bar – ghiboz Jul 12 '11 at 13:36
  • Ok, so you want a back button in your toolbar that only becomes active if you push some controller into a navigation stack. Is that it? – MiguelB Jul 12 '11 at 13:41
  • yes, I don't have a navigationbar, only a toolbar :) – ghiboz Jul 12 '11 at 13:45
  • 1
    I'll add my answer in a few minutes. Sit tight. – MiguelB Jul 12 '11 at 13:47

2 Answers2

2

You can't do it, at least not with the shape of a back button, the one with the arrow end on the left. backBarButtonItem is only a property of UINavigationItem.

You can either use a rectangular button on your toolbar to go back (although I don't think Apple is fond of that... you should read the iOS Human Interface Guidelines),

OR you can add a custom back button somewhere else on the screen. For instance, if your toolbar is at the bottom you can add a custom back button to the top-left corner of the screen to keep things tidy. This way, you would be saving the screen space of having a navigation bar just for the back button.

MiguelB
  • 1,913
  • 18
  • 20
  • With UIButton class, perhaps?... set its frame to wherever you need it to be, set its backgroundImage (with the shape of a back arrow, or something), etc. – MiguelB Jul 12 '11 at 14:04
1

typeonerror has code on github that contains what you need; it's actually for customising the back button on the a view controller, but you can use it to create a back button on a toolbar instead:

https://github.com/typeoneerror/BBCustomBackButtonViewController

In particular, code like the following (which actually came from stackoverflow, Customization of UINavigationBar and the back button ):

+ (UIBarButtonItem *)styledBackBarButtonItemWithTarget:(id)target selector:(SEL)selector;
{
   UIImage *image = [UIImage imageNamed:@"button_back"];
   image = [image stretchableImageWithLeftCapWidth:20.0f topCapHeight:20.0f];

   NSString *title = NSLocalizedString(@"Back", nil);
   UIFont *font = [UIFont boldSystemFontOfSize:12.0f];

   UIButton *button = [UIButton styledButtonWithBackgroundImage:image font:font title:title target:target selector:selector];
   button.titleLabel.textColor = [UIColor blackColor];

   CGSize textSize = [title sizeWithFont:font];
   CGFloat margin = (button.frame.size.height - textSize.height) / 2;
   CGFloat marginRight = 7.0f;
   CGFloat marginLeft = button.frame.size.width - textSize.width - marginRight;
   [button setTitleEdgeInsets:UIEdgeInsetsMake(margin, marginLeft, margin, marginRight)]; 
   [button setTitleColor:[UIColor colorWithRed:53.0f/255.0f green:77.0f/255.0f blue:99.0f/255.0f alpha:1.0f] forState:UIControlStateNormal];   

   return [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];
}
Community
  • 1
  • 1
JosephH
  • 37,173
  • 19
  • 130
  • 154