Kevin_TA
Kevin_TA

Reputation: 4675

Use Image for backBarButtonItem with NO TEXT

I'm simply trying to set the backBarButtonItem for my navigation controller to this image

enter image description here

instead of the Apple default arrow button whose title is the same as the previous view controller's title. The closest I've gotten so far is the above image stretched horizontally with the title still appearing overlaid. To get that, I used this code in my AppDelegate.

UIImage *backButtonImage = [UIImage imageNamed:@"back-button.png"];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonImage
                                                  forState:UIControlStateNormal
                                                barMetrics:UIBarMetricsDefault];

How can I get rid of the title (and prevent the button from being stretched)?

Upvotes: 2

Views: 3175

Answers (3)

Nitin Gohel
Nitin Gohel

Reputation: 49720

you can set custom Image of BarbuttonItem like :-

- (void)viewDidLoad 
{
    UIImage* imageRight = [UIImage imageNamed:@"Home_btn.png"];
    CGRect frameimgRight = CGRectMake(100, 100, 50, 30);

    RightBtn = [[UIButton alloc] initWithFrame:frameimgRight];
    [RightBtn setBackgroundImage:imageRight forState:UIControlStateNormal];
    [RightBtn addTarget:self action:@selector(ActionhomeBtn)
                       forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem *btnRight = [[UIBarButtonItem alloc]initWithCustomView:RightBtn];
    self.navigationItem.leftBarButtonItem = btnRight;

  [super viewDidLoad];

}

Look like:-

enter image description here

Hope its halp's you

Upvotes: 1

Simon Goldeen
Simon Goldeen

Reputation: 9080

When you use appearance, you are setting the background image of the back bar button items of your app. The fact that they have a background image has nothing to do with whether or not there is a title displayed on them. To use a custom bar button item instead of the default back item, look at this question.

In your case, you may want to not use appearance at all and instead create a bar button item as in the link above but use -initWithImage:style:target:action: instead of -initWithTitle:style:target:action:

Upvotes: 2

esreli
esreli

Reputation: 5061

I know this method works, and should be called in ViewDidLoad:

UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back-button.png"] style:UIBarButtonItemStyleBordered target:nil action:@selector(methodName)];

which then has to be assigned to the nav bar:

item.rightBarButtonItem = backButtonItem;

and then pushed:

[self.navBar pushNavigationItem:item animated:NO];

Upvotes: 0

Related Questions