theomen
theomen

Reputation: 919

Issue with back button background image

I have implemented this code in appDelegate in order to costumize navigation bar back button background image:

float version = [[[UIDevice currentDevice] systemVersion] floatValue];
    if (version >= 5.0)
    {
        // iPhone 5.0 code here
        UIImage *buttonImage = [[UIImage imageNamed:@"btn_backesp.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(1, 10, 1, 10)];
        [[UIBarButtonItem appearance] setBackButtonBackgroundImage:buttonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

    }

    return YES;

But I got this error, it seems that two back buttons are overlapped, the customized with the default one. I tried to hide left bar button item, and then both hide.

enter image description here

Many thanks

Upvotes: 0

Views: 1022

Answers (1)

Piyush Kashyap
Piyush Kashyap

Reputation: 1965

Use the code below to set back button image,It works perfect

UIButton *backBtn     = [UIButton buttonWithType:UIButtonTypeCustom];  
UIImage *backBtnImage = [UIImage imageNamed:@"btn_backesp.png"]  ;  
[backBtn setBackgroundImage:backBtnImage forState:UIControlStateNormal];  
[backBtn addTarget:self action:@selector(goback) forControlEvents:UIControlEventTouchUpInside];  
backBtn.frame = CGRectMake(0, 0, 54, 30);  
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithCustomView:backBtn] ;  
self.navigationItem.leftBarButtonItem = cancelButton;

goback method:-

- (void)goback
{
    [self.navigationController popViewControllerAnimated:YES];
}

Upvotes: 4

Related Questions