简体   繁体   中英

Distorted navigation bar custom back button

在此输入图像描述

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.

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]; 

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM