Nischal Hada
Nischal Hada

Reputation: 3288

Custom back button on navigation bar Not working

I have used following code instead of showing the image I set inbuilt arrow image is shown

 //Set Back button on Navigation
    [[UINavigationBar appearance] setBackIndicatorImage:[UIImage imageNamed:@"backBtn.png"]];
    [[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[UIImage imageNamed:@"backBtn.png"]];

Upvotes: 2

Views: 557

Answers (2)

Nimit Parekh
Nimit Parekh

Reputation: 16864

UIBarButtonItem *leftButtonItem = [self barItemWithTitle:@"Back" xOffset:11 target:self action:nil];
self.navigationItem.leftBarButtonItem = leftButtonItem;

Below method returns UIBarButtonItem for Navigation Bar.

- (UIBarButtonItem*)barItemWithTitle:(NSString*)title xOffset:(NSInteger)xOffset target:(id)target action:(SEL)action
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
    [button.titleLabel setFont:[UIFont systemFontOfSize:18]];
    [button addTarget:target action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    CGSize size = [title sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:18.0f]}];
    CGSize adjustedSize = CGSizeMake(ceilf(size.width), ceilf(size.height));
    CGRect rect = CGRectMake(0, 0,adjustedSize.width + 3,24);
    [button setFrame:rect];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
        [button setContentEdgeInsets:UIEdgeInsetsMake(0, xOffset, 0, -xOffset)];
    }

    UIBarButtonItem *customUIBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    return customUIBarButtonItem;
}

Action Method

-(void)buttonAction{
     //Your code goes here.
}

May this help lot.

Upvotes: 1

soumya
soumya

Reputation: 3811

Hope this helps you..

[[UIBarButtonItem appearance] setBackButtonBackgroundImage:[UIImage imageNamed:@"backBtn.png"]
                                                  forState:UIControlStateNormal
                                                barMetrics:UIBarMetricsDefault];

Upvotes: 1

Related Questions