Luciano Nascimento
Luciano Nascimento

Reputation: 2600

Remove leftBarButtonItem Background

How to remove leftBarButtonItem blue background? I tried style:UIBarButtonItemStylePlain but had no result.

Code:

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_home"] style:UIBarButtonItemStylePlain target:self.navigationController action:@selector(openMenu) ];

enter image description here

Upvotes: 2

Views: 652

Answers (4)

Divyam shukla
Divyam shukla

Reputation: 2048

UIButton *btn= [UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(0,0,30, 40)];    
[btn setImage:[UIImage imageNamed:@"icon_home.png"] forState:UIControlStateNormal];
[self.navigationItem setLeftBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:btn]];

This Will work please try....

Upvotes: 0

axiixc
axiixc

Reputation: 1972

If you're only concerned with iOS 5+ there is another way that doesn't involve custom views. With the UIAppearance additions, you can use

UIImage * emptyImage() {
    UIGraphicsBeginImageContext(CGSizeMake(1, 1));
    UIImage * emptyImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return emptyImage;
}

UIBarButtonItem * item = [[UIBarButtonItem alloc] initWith...];
[item setBackgroundImage:emptyImage() forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
self.navigationItem.leftBarButtonItem = item;

and if this is an app-wide style, just do

[[UIBarButtonItem appearance] setBackgroundImage:emptyImage() forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

once in your AppDelegate and you won't even need to fuss with individual instances.

Upvotes: 0

Manu
Manu

Reputation: 4750

    UIButton *tempButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [tempButton setFrame:CGRectMake(2, 1, 34, 34)]; // your Home Button Image width and height.
    [tempButton addTarget:self action:@selector(btnBackClicked:) forControlEvents:UIControlEventTouchUpInside];
    [tempButton setImage:[UIImage imageNamed:@"icon_home.png"] forState:UIControlStateNormal];
    [tempButton setImage:[UIImage imageNamed:@"icon_home_h.png"] forState:UIControlStateHighlighted];
    [self.navigationItem setLeftBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:tempButton]];

Upvotes: 8

Michael Reneer
Michael Reneer

Reputation: 2571

The in your case UINavigationController is doing some additional styling when managing it's navigationBar and the UIBarButtonItems in it. So setting the style to plain will not behave the same as it does when working with a UIToolbar.

What you need is a custom view, something like...

UIImage *image = [UIImage imageNamed:@"icon_home"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:imageView];
barButtonItem.target = self;
barButtonItem.action = @selector(openMenu);
self.navigationItem.leftBarButtonItem = barButtonItem;

Also, you will probably want target to set to self not self.navigationController.

Upvotes: 1

Related Questions