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



I'd love to create a "back" left-arrow-bezel button in a UIToolbar.

As far as I can tell, the only way to get one of these is to leave UINavigationController at default settings and it uses one for the left bar item. But there's no way I can find to create one as a UIBarButtonItem, so I can't make one in a standard UIToolbar, even though they're very similar to UINavigationBars.

I could manually create it with button images, but I can't find the source images anywhere. They have alpha-channel edges, so screenshotting and cutting won't get very versatile results.

Any ideas beyond screenshotting for every size and color scheme I intend to use?

Update: PLEASE STOP dodging the question and suggesting that I shouldn't be asking this and should be using UINavigationBar. My app is Instapaper Pro. It shows only a bottom toolbar (to save space and maximize readable content area), and I wish to put a left-arrow-shaped Back button in the bottom.

Telling me that I shouldn't need to do this is not an answer and certainly doesn't deserve a bounty.


UIWebView scrollBy replacement

1:

Hide the tab bar when displaying the ABPeoplePickerNavigationController?
I used the following psd this I derived from http://www.teehanlax.com/blog/?p=447. Draw a shadow behind UIWebView http://www.chrisandtennille.com/pictures/backalthough ton.psd. iPhone subview design (UIView vs UIViewController) I then just created a custom UIView this I use in the customView property of the toolbar item.. Inability to delete table cells after a certain point Works well for me.. Is there an Cocoa or Objective C api for Java? Edit: As pointed out by PrairieHippo, maralbjo found this using the following, simple code did the trick (requires custom image in bundle) should be combined with this answer. Protocols with optional properties?So here is additional code:. How to determine where this memory leak is coming from?
// Creates a back although ton instead of default behaviour (displaying title of previous screen) UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back_arrow.png"]                                                                style:UIBarButtonItemStyleBordered                                                               tarreceive :self                                                               action:@selector(backAction)];  tipsDetailViewController.navigationItem.leftBarButtonItem = backButton; [backButton release]; 

2:

The Unicode Method

I think it is enough easier to just use a unicode character to receive the job done. You must 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 brule I add the 0xFE0E Unicode Variation Selector. .
NSString *backArrowString = @"\U000025C0\U0000FE0E"; //BLACK LEFT-POINTING TRIANGLE PLUS VARIATION SELECTOR  UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:backArrowString style:UIBarButtonItemStylePlain tarreceive :nil action:nil]; self.navigationItem.leftButtonItem = backBarButtonItem; 
The code block is just for the demo. It would job in any although ton this 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.

3:

WARNING: There are reports this this will not job on iOS 6. This might only job 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 must be done without adding in your own image files using sekr1t although ton type 101 to receive the correct shape. For me the trick was figuring out this I could use 'initWithCustomView' to create the Bar Button Item. I personally needed this for a dynamic navbar rather than a toolbar, although I tested it with a toolbar and the code is nearly the same:.
// create although ton UIButton* backButton = [UIButton although tonWithType:101]; // left-pointing shape! [backButton addTarreceive :self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside]; [backButton setTitle:@"Back" forState:UIControlStateNormal];  // create although ton item -- possible for the reason this 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, although this depends on the rest of your code and I leave this 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 although ton types and this isn't one of them..

4:

enter image description here. First of all you have to find an image of the back although ton. I used a nice app called Extractor this extracts all the graphics from iPhone. In iOS7 I managed to retrieve the image called UINavigationBarBackIndicatorDefault and it was in black colour, since I needed a red tint I change the colour to red using Gimp.. EDIT: . As was mentioned by btate in his comment, there is no need to change the color with the image editor. The following code should did the trick:.
imageView.tint = [UIColor redColor]; imageView.image = [[UIImage imageNamed:@"UINavigationBarBackIndicatorDefault"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; 
Then I created a view this contains an imageView with this arrow, a label with the custom text and on top of the view I have a although ton with an action. Then I added a simple animation (fading and translation).. The following code simulates the behaviour of the back although ton including animation..
-(void)viewWillAppear:(BOOL)animated{         UIImageView *imageView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"UINavigationBarBackIndicatorDefault"]];         [imageView setTintColor:[UIColor redColor]];         UILabel *label=[[UILabel alloc] init];         [label setTextColor:[UIColor redColor]];         [label setText:@"Blog"];         [label sizeToFit];          int space=6;         label.frame=CGRectMake(imageView.frame.origin.x+imageView.frame.size.width+space, label.frame.origin.y, label.frame.size.width, label.frame.size.height);         UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, label.frame.size.width+imageView.frame.size.width+space, imageView.frame.size.height)];          view.bounds=CGRectMake(view.bounds.origin.x+8, view.bounds.origin.y-1, view.bounds.size.width, view.bounds.size.height);         [view addSubview:imageView];         [view addSubview:label];          UIButton *although ton=[[UIButton alloc] initWithFrame:view.frame];         [although ton addTarreceive :self action:@selector(handleBack:) forControlEvents:UIControlEventTouchUpInside];         [view addSubview:although ton];          [UIView animateWithDuration:0.33 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{             label.alpha = 0.0;             CGRect orig=label.frame;             label.frame=CGRectMake(label.frame.origin.x+25, label.frame.origin.y, label.frame.size.width, label.frame.size.height);             label.alpha = 1.0;             label.frame=orig;         } completion:nil];          UIBarButtonItem *backButton =[[UIBarButtonItem alloc] initWithCustomView:view]; }  - (void) handleBack:(id)sender{ } 
EDIT:. Instead of adding the although ton, in my opinion the better approach is to use a gesture recognizer. .
UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarreceive :self action:@selector(handleBack:)]; [view addGestureRecognizer:tap]; [view setUserInteractionEnabled:YES]; 

5:

I'm not sure if this would work, although you could try creating a UINavigationController with the default settings to create the although ton, find the although ton in the navigation controller's subview hierarchy, call removeFromSuperview on it, destroy the navigation controller, and then add the although ton as a subview of your toolbar. You may also need to retain and the although ton before calling removeFromSuperview (and then release it after adding it as subview of your toolbar) to avoid it being deallocated during the process..

6:

To make a UIButton with an arrow pretty close (I'm not a designer ;) to the iOS 7 system back arrow:. Standard: . Standard system back arrow . Apple SD Gothic Neo. Apple SD Gothic Neo < character. In Xcode:.
  • Focus on the title value field of the although ton (or any another view/control with text content)
  • Open Edit -> Special Characters
  • Select the Parentheses group and double click the '<' character
  • Change font to: Apple SD Gothic Neo, Regular with desired size (e.g. 20)
  • Change colour as you like
Ref @staticVoidMan's answer to Back-like arrow on iOS 7.

7:

    self.navigationItem.leftBarButtonItem = self.navigationItem.backBarButtonItem; 
Works for me. I used this when I had more tabs then could fit on the tab bar, and a view controller pushed from the "More" overrode the leftBarButtonItem in its viewDidLoad..

8:

You must find the source images by extracting them from Other.artjob in UIKit ${SDKROOT}/System/Library/Frameworks/UIKit.framework/Other.artwork. The modding community has any tools for extracting them, here. Once you extract the image you must write any code to recolor it as necessary and set it as the although ton image. Whether or not you must actually ship such a thing (since you are embedding derived artwork) might be a little dicey, so maybe you want to talk to a lawyer..

9:

The Three20 library has a way to did this:.
  UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle: @"Title" style:UIBarButtonItemStylePlain                                                                  tarreceive :self action:@selector(foo)];    UIColor* darkBlue = RGBCOLOR(109, 132, 162);    TTShapeStyle* style = [TTShapeStyle styleWithShape:[TTRoundedLeftArrowShape shapeWithRadius:4.5] next:     [TTShadowStyle styleWithColor:RGBCOLOR(255,255,255) blur:1 offset:CGSizeMake(0, 1) next:     [TTReflectiveFillStyle styleWithColor:darkBlue next:     [TTBevelBorderStyle styleWithHighlight:[darkBlue shadow]                                      shadow:[darkBlue multiplyHue:1 saturation:0.5 value:0.5]                                       width:1 lightSource:270 next:     [TTInsetStyle styleWithInset:UIEdgeInsetsMake(0, -1, 0, -1) next:     [TTBevelBorderStyle styleWithHighlight:nil shadow:RGBACOLOR(0,0,0,0.15)                                         width:1 lightSource:270 next:nil]]]]]];    TTView* view = [[[TTView alloc] initWithFrame:CGRectMake(0, 0, 80, 35)] autorelease];   view.backgroundColor = [UIColor clearColor];   view.style = style;   backButton.customView = view;     self.navigationItem.leftBarButtonItem = backButton; 

10:

I found this using the following, simple code did the trick (requires custom image in bundle): .
// Creates a back although ton instead of default behaviour (displaying title of previous screen)     UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back_arrow.png"]                                                                    style:UIBarButtonItemStyleBordered                                                                   tarreceive :self                                                                   action:@selector(backAction)];      tipsDetailViewController.navigationItem.leftBarButtonItem = backButton;     [backButton release]; 

11:

Here's what I ended up doing after searching through all these solutions and others. It uses a stretchable png's extracted from the UIKit stock images. This way you must set the text to whatever you liek.
// Generate the background images UIImage *stretchableBackButton = [[UIImage imageNamed:@"UINavigationBarDefaultBack.png"] stretchableImageWithLeftCapWidth:14 topCapHeight:0]; UIImage *stretchableBackButtonPressed = [[UIImage imageNamed:@"UINavigationBarDefaultBackPressed.png"] stretchableImageWithLeftCapWidth:13 topCapHeight:0]; // Setup the UIButton UIButton *backButton = [UIButton although tonWithType:UIButtonTypeCustom]; [backButton setBackgroundImage:stretchableBackButton forState:UIControlStateNormal]; [backButton setBackgroundImage:stretchableBackButtonPressed forState:UIControlStateSelected]; NSString *although tonTitle = NSLocalizedString(@"Back", @"Back"); [backButton setTitle:although tonTitle forState:UIControlStateNormal]; [backButton setTitle:although tonTitle forState:UIControlStateSelected]; backButton.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 2, 1); // Tweak the text position NSInteger width = ([backButton.titleLabel.text sizeWithFont:backButton.titleLabel.font].width + backButton.titleEdgeInsets.right +backButton.titleEdgeInsets.left); [backButton setFrame:CGRectMake(0, 0, width, 29)]; backButton.titleLabel.font = [UIFont boldSystemFontOfSize:13.0f]; [backButton addTarreceive :self action:@selector(yourSelector:) forControlEvents:UIControlEventTouchDown]; // Now add the although ton as a custom UIBarButtonItem UIBarButtonItem *backButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:backButton] autorelease]; self.navigationItem.leftBarButtonItem = backButtonItem; 

12:

It's easy to did with Interface Builder in Xcode 5.x. Result .
  • use Toolbar and Bar Button Item from Object library. Components .
  • in although ton's Attrialthough es inspector edit Image section with your back although ton image. Attrialthough es inspector.
Done!.

13:

back arrow. If you don't want to banother with image files, the arrow shape must be drawn in a UIView subclass with the following code:.
- (void)drawRect:(CGRect)rect {     float width = rect.size.width;     float height = rect.size.height;     CGContextRef context = UIGraphicsGetCurrentContext();      CGContextBeginPath(context);     CGContextMoveToPoint(context, width * 5.0/6.0, height * 0.0/10.0);     CGContextAddLineToPoint(context, width * 0.0/6.0, height * 5.0/10.0);     CGContextAddLineToPoint(context, width * 5.0/6.0, height * 10.0/10.0);     CGContextAddLineToPoint(context, width * 6.0/6.0, height * 9.0/10.0);     CGContextAddLineToPoint(context, width * 2.0/6.0, height * 5.0/10.0);     CGContextAddLineToPoint(context, width * 6.0/6.0, height * 1.0/10.0);     CGContextClosePath(context);      CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor);     CGContextFillPath(context); } 
where the arrow view is proportional to a width of 6.0 and a height of 10.0.

14:

To create an image for the UIToolbar, make a png in photoshop and WHERE EVER there is ANY colour it puts it white, and where it's alpha = 0 then it leaves it alone.. The SDK actually put's the brule around the icon you have made and turns it white without you having to did anything.. See, this is what I made in Photoshop for my forward although ton (obviously swap it around for back although ton):. http://twitpic.com/1oanv. and this is what it appeared like in Interface Builder. http://twitpic.com/1oaoa.

15:

I was endeavor to did the same thing, although I wanted the back although ton to be in the navigation bar. (I actually needed a back although ton, this would did more than only going back, so I had to use the leftBarButtonItem property). I tried what AndrewS suggested, although in the navigation bar it wouldn't look the way it should, as the UIButton was kind of casted to a UIBarButtonItem.. But I found a way to job around this. I actually just put a UIView under the UIButton and set the customView for the UIBarButtonItem. Here is the code, if any body needs it:.
// initialize although ton and although ton view UIButton *backButton = [UIButton although tonWithType:101]; UIView *backButtonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, backButton.frame.size.width, backButton.frame.size.height)];  [backButton addTarreceive :self action:@selector(backButtonTouched:) forControlEvents:UIControlEventTouchUpInside]; [backButton setTitle:@"Back" forState:UIControlStateNormal]; [backButtonView addSubview:backButton];  // set although tonview as custom view for bar although ton item UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButtonView]; self.navigationItem.leftBarButtonItem = backButtonItem;  // push item to navigationbar items [self.navigationController.navigationBar setItems:[NSArray arrayWithObject:backButtonItem]]; 

16:

Solution WITHOUT using a png. Based on this SO answer: Adding the little arrow to the right side of a cell in an iPhone TableView Cell. Just flipping the UITableViewCell horizontally!.
UIButton *btnBack = [[UIButton alloc] initWithFrame:CGRectMake(0, 5, 70, 20)];  // Add the disclosure CGRect frm; UITableViewCell *disclosure = [[UITableViewCell alloc] init]; disclosure.transform = CGAffineTransformScale(CGAffineTransformIdentity, -1, 1); frm = self.btnBack.bounds; disclosure.frame = CGRectMake(frm.origin.x, frm.origin.y, frm.size.width-25, frm.size.height); disclosure.accessoryType = UITableViewCellAccessoryDisclosureIndicator; disclosure.userInteractionEnabled = NO; [self.btnBack addSubview:disclosure];  // Add the label UILabel *lbl = [[UILabel alloc] init]; frm = CGRectOffset(self.btnBack.bounds, 15, 0); lbl.frame = frm; lbl.textAlignment = NSTextAlignmentCenter; lbl.text = @"BACK"; [self addSubview:lbl]; 

17:

If you want to avoid drawing it yourself, you could use the undocumented class: UINavigationButton with style 1. This could, of course, stop your application from being approved... /John.

18:

Well, you don't have to have a different although ton for every size, you must use [UIImage stretchableImageWithLeftCapWidth:topCapHeight:], although the only thing I've found is custom images..

19:

I had a similar problem, and come out one library PButton. And the sample is the back navigation although ton like although ton, which must be used anywhere just like a customized although ton. . Something like this: enter image description here.

20:

Try this. I am sure you did not need a back although ton image to create one such..
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back"                                                                style:UIBarButtonItemStyleBordered                                                               tarreceive :self                                                               action:@selector(yourSelectorGoesHere:)]; self.navigationItem.backBarButtonItem = backButton; 
That's all you have to did :).

21:

Why are you doing this? If you want any thing this looks like a navigation bar, use UINavigationBar.. Toolbars have specific visual style associated with them. The Human Interface Guidelines for the iPhone state:.
A toolbar appears at the bottom edge of the screen and contains although tons this perform actions related to objects in the current view..
It then gives several visual examples of roughly square icons with no text. I would urge you to follow the HIG on this..


66 out of 100 based on 51 user ratings 326 reviews