2

How can I make a customize back button without Text in iOS using barmetrics?

I wanna make something like http://a397.phobos.apple.com/us/r1000/081/Purple/v4/e6/be/2d/e6be2d9e-dc95-7e44-b1ed-9386fa9f4d02/mzl.zwjkpepo.320x480-75.jpg

Sam Axe
  • 33,313
  • 9
  • 55
  • 89
Dixit Chopra
  • 77
  • 2
  • 10
  • This is an oft asked question! Check out: http://www.verious.com/component/custom-back-button/ http://stackoverflow.com/questions/9300503/make-a-custom-back-button-for-uinavigationcontroller – Forhad Ahmed Feb 08 '13 at 18:52

3 Answers3

7
[[UIBarButtonItem appearance]
            setBackButtonBackgroundImage:[UIImage imageNamed:@"back_button.png"]
            forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

You can place that in your app delegate and it will set the background image to all back buttons in the app (for that control state and bar metrics, of course).

Edit: If you want to something different then use this code:

- (void)setBackButton
{
    UIButton *backButton =  [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton setImage:[UIImage imageNamed:@"BackButton.png"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backButtonTapped:) forControlEvents:UIControlEventTouchUpInside];[button setFrame:CGRectMake(0, 0, 32, 32)];

    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:backButton] autorelease];
}

- (void)backButtonTapped:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}  
aks.knit1108
  • 1,305
  • 9
  • 20
  • Did you try it? I tried this. The image is coming but its taking the full size of back button rather than the image size. "Back" is also getting displayed :( – Dixit Chopra Feb 08 '13 at 18:59
  • You can use second approach, for this you can find so many solution on google. – aks.knit1108 Feb 08 '13 at 19:11
  • Follow this link http://stackoverflow.com/questions/1998438/custom-back-button-image-on-the-navigation-bar-doesnt-work-iphone-sdk – aks.knit1108 Feb 08 '13 at 19:14
  • This workaround is not 100% good as it removes the swipe transition for the UINavigationController – Andrespch Aug 03 '15 at 14:05
0

How about Something like this?

UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(YourFrame)];

[backButton setTitle:@"<" forState:UIControlStateNormal];

[backButton addTarget:self action:@selector(callSelectorMethod) forControlEvents:UIControlEventTouchUpInside];

self.navigationItem.rightBarButtonItem  = [[[UIBarButtonItem alloc] initWithCustomView:backButton] autorelease];

[backButton release],backButton=nil;
Reno Jones
  • 1,979
  • 1
  • 18
  • 30
0

Here is the solution:

 UIButton *button =  [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:@"BackButton.png"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(popBack) forControlEvents:UIControlEventTouchUpInside];
    [button setFrame:CGRectMake(0, 0, 32, 32)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];

Thanks to Best Coder

Community
  • 1
  • 1
Dixit Chopra
  • 77
  • 2
  • 10