UIBackButton Background Image not appearing

2019-02-17 04:14发布

问题:

i'm having a problem with my UIBackbutton not appearing and i'm not sure what is causing it.

This is my code for making the back button image, it is in my first ViewController :

UIImage *backButtonHomeImage = [[UIImage imageNamed:@"backButtonImage"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonHomeImage  forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

When you first enter the next view (done by a segue from a button) the button isn't visible but the text to go back is visible. Once i've hit the back button the image appears and it stays appeared on the next time i click it.

I'm not sure if this is an issue to do with my code, or the file it's in or if it's and iOS 7 problem.

回答1:

You may have more luck using a UIBarButtonItem and setting the back button background explicitly.

The appearance proxy has been unreliable in my own attempts to get a back button working with iOS 7. I experienced the same issue with the background not showing up properly first time showing the button.



回答2:

If you just need to change the colour, try this[[UINavigationBar appearance] setTintColor:[UIColor redColor]];



回答3:

If you want to change back button image you just need to write this 2 lines of code:

[self.navigationBar setBackIndicatorImage:[UIImage imageNamed:@"icon-back"]];
[self.navigationBar setBackIndicatorTransitionMaskImage:[UIImage imageNamed:@"icon-back"]];

And if you create sub class of UINavigationController it makes easy to customize you navigationbar and push & pop controllers



回答4:

Try this:

self.navigationItem.hidesBackButton = NO;

if you're trying to add it to the Navigation bar then:

 UIImage* image4 = [UIImage imageNamed:@".....png"];
    button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 26, 26)];
    [button setBackgroundImage:....png forState:UIControlStateNormal];
    [button addTarget:self action:@selector(Selector)
         forControlEvents:UIControlEventTouchUpInside];
    [button setShowsTouchWhenHighlighted:YES];

    UIBarButtonItem *action = [[UIBarButtonItem alloc] initWithCustomView:button];
    NSArray *arr =[[NSArray alloc]initWithObjects:action, nil];
    self.navigationItem.rightBarButtonItems = arr;