Setting BackBarButtonItem

I am trying to configure backBarButtonItem with a custom image (the text "back" is included in this image), here is the current result:

http://i.imgur.com/rFxFt.png

Does anyone know why this might happen?

Here is my code in viewDidLoad (actually works on both the parent controller and the new controller with the back button)

UIImage *backButtonImage = [UIImage imageNamed:@"Graphics/Shared/navigation_back_button.png"]; UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:backButtonImage style:UIBarButtonItemStylePlain target:nil action:nil]; self.navigationItem.backBarButtonItem = backButton; 

Edit: I am using iOS 5 by the way! Maybe the look of the proxy can be used, but so far, when I tried to use it for the back button button (in appDelegate), the application will just work.

+7
source share
4 answers

Ok, I solved the problem using a bit of a rough trick, but at least it works. If someone comes up with a more standard solution, please let me know!

Here is my code:

 UIImage *backButtonImage = [[UIImage imageNamed:@"Graphics/Shared/navigation_back_button.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)]; [[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault]; [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, backButtonImage.size.height*2) forBarMetrics:UIBarMetricsDefault]; 

This is in my appdelegate method:

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 

To stop stretching when used as a background, I used the idea of iOS 5: How to implement a custom (back) back button

But then changed it, instead of setting -

 self.title = @" "; 

Each time the view is loaded (and this may also go bad with the navigation bar title itself)

I just set the offset for the back button text to twice the height of the image, so you will never see it.

Why move on to all these troubles and not use the left button element with its own method to pull out the view controller?

The main reason is that you lose the standard sliding animation back to change views. In addition, this means that I do not need to use a custom button or write my own method to return. It just works.

Hope this solves the problem of another too, I know that I got stuck on it for 3 hours!

+16
source

The designation " UIBarButtonItemStylePlain " indicates the style of the button.

Try creating the back button as follows:

 UIImage *backButtonImage = [UIImage imageNamed:@"Graphics/Shared/navigation_back_button.png"]; CGRect frameimg = CGRectMake(0, 0, backButtonImage.size.width, backButtonImage.size.height); UIButton *someButton = [[UIButton alloc] initWithFrame:frameimg]; [someButton setBackgroundImage:backButtonImage forState:UIControlStateNormal]; // you have to do your own backButtonAction to pop the view controller, btw [someButton addTarget:self action:@selector(backButtonAction:) forControlEvents:UIControlEventTouchUpInside]; [someButton setShowsTouchWhenHighlighted:YES]; UIBarButtonItem *backButton =[[UIBarButtonItem alloc] initWithCustomView:someButton]; [someButton release]; self.navigationItem.backBarButtonItem = backButton; 
0
source

In this case, it was easier for me to replace barButtonItem, simply using a custom button.

Not quite sure how you designed the project, but it worked for me.

The setting for the custom button

The back button, resulting look

Then attach this code to it:

 //go back to the previous screen -(IBAction)back{ [self dismissModalViewControllerAnimated:YES]; } 
0
source

I tried a lot today and got this solution. It separates the background image and the button image , it seems simpler and not so complicated.

 UIImage *bgImage = [UIImage imageNamed:@"the-same-image-as-your-navbar-background-image"]; [[UIBarButtonItem appearance] setBackButtonBackgroundImage:bgImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault]; [[UIBarButtonItem appearance] setBackButtonBackgroundImage:bgImage forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault]; UIImage *buttonImage = [UIImage imageNamed:@"your-back-button-image"]; UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:buttonImage style:UIBarButtonItemStylePlain target:nil action:nil]; self.navigationItem.backBarButtonItem = backButton; 
0
source

All Articles