Apollo
Apollo

Reputation: 9064

Setting UIBarButtonItem to image with no background

Currently I'm setting my UIBarButtonItem to a 40x40 px image with a transparent background. When I go to set the UIBarButtonItem to this image, it produces the result below:

enter image description here

I just want the image of the guy without the green background. How should I go about doing this?

Code I've Tried:

 self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"profile-barbutton.png"] style:UIBarButtonItemStylePlain target:self.navigationController action:@selector(myselector)];

Also tried the following (as well as initializing the bar with initWithTitle:@"" ...

Worked But Selector No Longer References UINavigationController

The following code produces the exception [MasterViewController toggleMenu]: unrecognized selector because the selector is inside my NavigationController class, which was previously referenced with the UIBarButtonItem but isn't any longer.

      UIButton *profileBarButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40.0f, 40.0f)];
    [profileBarButton setImage:[UIImage imageNamed:@"profile-barbutton.png"] forState:UIControlStateNormal];
    [profileBarButton addTarget:self action:@selector(toggleMenu) forControlEvents:UIControlEventTouchUpInside];



    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:profileBarButton];

    [self.navigationItem.leftBarButtonItem setTarget:self.navigationController];
    [self.navigationItem.leftBarButtonItem setAction:@selector(toggleMenu)];

Upvotes: 0

Views: 946

Answers (1)

Wilson
Wilson

Reputation: 56

You want to set the background image, not the image. Try this:

[self.navigationItem.leftBarButtonItem setBackgroundImage:[UIImage imageNamed:@"profile-barbutton.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault]; 

Upvotes: 1

Related Questions