-1

In a storyboard, I create a navigation bar by using "Embed in".

I'm trying to replace the original image of back button, which is like this

enter image description here

by my back button image, which is

enter image description here

How to do this?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Zheng
  • 77
  • 1
  • 11

4 Answers4

2

In your controllers viewDidLoad method add following code..

UIImage *leftbuttonImage = [UIImage imageNamed:@"yourimagename"];
        UIButton *leftbutton = [UIButton buttonWithType:UIButtonTypeCustom];
        [leftbutton setImage:leftbuttonImage forState:UIControlStateNormal];
        leftbutton.frame = CGRectMake(0, 0, 35, 35);
        [leftbutton addTarget:self action:@selector(showLeftMenuPressed:) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *customBarleftItem = [[UIBarButtonItem alloc] initWithCustomView:leftbutton];

        self.navigationItem.leftBarButtonItem = customBarleftItem;
Ashish P.
  • 848
  • 5
  • 12
  • Hi, thank you for replying. In line "[leftbutton addTarget:self action:@selector(showLeftMenuPressed:) forControlEvents:UIControlEventTouchUpInside];", xcode says showLeftMenuPressed is not declared. How can I get rid of it? – Zheng Nov 01 '13 at 12:11
  • 1
    sorry to not mention that.. replace showLeftMenuPressed: method by your method which you want to call on back button click. – Ashish P. Nov 01 '13 at 12:14
  • Notice that if you replace your back button, then the swipe gesture to go back in iOS 7 stops working. Some solutions here: http://stackoverflow.com/questions/19054625/changing-back-button-in-ios-7-disables-swipe-to-navigate-back – Daniel Martín Nov 01 '13 at 12:22
2
UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];  
UIImage *backBtnImage = [UIImage imageNamed:@"BackBtn.png"]  ;  
[backBtn setBackgroundImage:backBtnImage forState:UIControlStateNormal];  
[backBtn addTarget:self action:@selector(goback) forControlEvents:UIControlEventTouchUpInside];  
backBtn.frame = CGRectMake(0, 0, 54, 30);  
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:backBtn] ;  
self.navigationItem.leftBarButtonItem = backButton;

and declare goback:

- (void)goback
{
    [self.navigationController popViewControllerAnimated:YES];
}
Nikos M.
  • 13,685
  • 4
  • 47
  • 61
2
-(void)setUpNAvigationBackBarButton
{

    UIImage* image3 = [UIImage imageNamed:@"icon_back.png"];
    CGRect frameimg = CGRectMake(0, 0, image3.size.width, image3.size.height);
    UIButton *someButton = [[UIButton alloc] initWithFrame:frameimg];
    [someButton setBackgroundImage:image3 forState:UIControlStateNormal];
    [someButton addTarget:self action:@selector(YourMethod which you wanna call on back button
         forControlEvents:UIControlEventTouchUpInside];


    UIBarButtonItem *mailbutton =[[UIBarButtonItem alloc] initWithCustomView:someButton];
    [self.navigationItem setLeftBarButtonItem:mailbutton animated:YES];
}

just call this function in your viewdidLoad. I have used same this function in ma code. working fine.. you can use it.

BhavikKama
  • 8,566
  • 12
  • 94
  • 164
0

Swift version :-

self.navigationController?.navigationBar.backIndicatorImage = UIImage(named: "HomeLeft@2x")
self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = UIImage(named: "HomeLeft@2x")
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: UIBarButtonItemStyle.Plain, target: nil, action: nil)

put this on viewDidLoad( )

Mudith Chathuranga Silva
  • 7,253
  • 2
  • 50
  • 58