Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a back arrow to leftBarButtonItem?

I want to add a back arrow to a leftBarButtonItem, to make it appear visually as if it was a regular back button, although it's functionality is slightly different.

Is there a way to do this?

like image 523
Andrew Avatar asked Jan 07 '14 12:01

Andrew


2 Answers

You can use custom button and hide the back button if you are using navigation controller.

To hide back button use this:

self.navigationItem.hidesBackButton = YES;

And for Custom Button:

UIButton * customButton = [UIButton buttonWithType:UIButtonTypeCustom];
[customButton setBackgroundColor:[UIColor colorWithRed:197.0/255.0 green:190.0/255.0 blue:157.0/255.0 alpha:1.0]];
[customButton setTitle:@"Do Something" forState:UIControlStateNormal];
customButton.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:11.0f];
[customButton.layer setCornerRadius:3.0f];
[customButton.layer setMasksToBounds:YES];
[customButton.layer setBorderWidth:1.0f];
[customButton.layer setBorderColor: [[UIColor grayColor] CGColor]];
customButton.frame=CGRectMake(0.0, 100.0, 50.0, 25.0);
[customButton addTarget:self action:@selector(customMethod:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem * customItem = [[UIBarButtonItem alloc] initWithCustomView:customButton];
customItem.tintColor=[UIColor blackColor];
self.navigationItem.leftBarButtonItem = customItem;

This will work for you. Happy Coding

like image 185
Raj Shekhar Avatar answered Oct 14 '22 09:10

Raj Shekhar


If you are using UINavigationController and then push new viewcontroller it will automatically show back button. If you are Not Using UINavigationController than

Use Custom UIButton for that with back image

UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
backButton.frame = CGRectMake(0, 0, 48, 37);
[backButton addTarget:self action:@selector(backButtonTapped) forControlEvents:UIControlEventTouchUpInside];
backButton.showsTouchWhenHighlighted = YES;

UIImage *backButtonImage = [UIImage imageNamed:@"back-button.png"];
[backButton setImage:backButtonImage forState:UIControlStateNormal];

backButton.imageEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10);

UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = backBarButtonItem;

You can change the value for the frame and the imageEdgeInsets as per your requirements.

Refer this SO Answer.

like image 44
Toseef Khilji Avatar answered Oct 14 '22 09:10

Toseef Khilji