prabhu
prabhu

Reputation: 978

Distorted navigation bar custom back button

enter image description here

I have a custom back button and the button is not displaying as intended, it is being stretched, and even though the back button text is empty it is still displaying the "back" text.

Thanks in advance.

Upvotes: 3

Views: 588

Answers (2)

Mark Kryzhanouski
Mark Kryzhanouski

Reputation: 7241

I usually use this technique to make it work:

  1. To get rid default title:

    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(-400.f, 0) forBarMetrics:UIBarMetricsDefault];
    
  2. Use resizable image:

    UIImage *backButton = [UIImage imageNamed:@"back.png"];         
    [[UIBarButtonItem appearance] setBackButtonBackgroundImage:[backButton resizableImageWithCapInsets:UIEdgeInsetsMake(0, backButton.size.width, 0, 0)] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    

Upvotes: 4

Jano
Jano

Reputation: 63667

Try something like

UIButton *back = [UIButton buttonWithType:UIButtonTypeCustom];  
UIImage *image = ... your image
[back setBackgroundImage:image forState:UIControlStateNormal];  
[back addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
back.frame = CGRectMake(0, 0, image.size.width, image.size.height);
UIBarButtonItem *backbi = [[UIBarButtonItem alloc] initWithCustomView:back];  
self.navigationItem.leftBarButtonItem = backbi;

or post your code.

Upvotes: 2

Related Questions