Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom UIBarButtonItem

I am trying to create a custom UIBarbuttonItem that uses just a png with transparency so that I only have an icon as button. When I try to set the button image, set the background as white, and set the style to Plain I still get an inner shadow and black border around it.

What gives?

enter image description here

I have tried the below code and it still puts the black border around it.

UIImage *background = [UIImage imageNamed:@"Dismiss_normal.png"];
UIImage *backgroundSelected = [UIImage imageNamed:@"Dismiss_selected.png"];
self.closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.closeButton addTarget:self action:@selector(closeButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; //adding action
[self.closeButton setBackgroundImage:background forState:UIControlStateNormal];
[self.closeButton setBackgroundImage:backgroundSelected forState:UIControlStateSelected];
self.closeButton.frame = CGRectMake(0 ,0,background.size.width, background.size.height);
self.closeButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.closeButton];
self.navigationItem.leftBarButtonItem = self.closeButtonItem;

What I noticed is if I do a modal segue the button, with the code above still has a black border around it, but if I do a push segue it doesn't? WTF?

like image 783
jdog Avatar asked Jun 07 '13 04:06

jdog


2 Answers

You must set the button type to Custom and the icon image to the button background. example code:

UIImage *background = [UIImage imageNamed:@"icon.png"];
UIImage *backgroundSelected = [UIImage imageNamed:@"icon_selected.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:@selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside]; //adding action
[button setBackgroundImage:background forState:UIControlStateNormal];
[button setBackgroundImage:backgroundSelected forState:UIControlStateSelected];
button.frame = CGRectMake(0 ,0,35,35);

Then set this button to your BarButtonItem like this:

UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = barButton;
like image 134
Primoz990 Avatar answered Sep 29 '22 13:09

Primoz990


I understood your problem.

When you actually push a UIViewController it will be a part of existing UINavigationController. So the UINavigationBar retains. But when you use ModalViewController its just another UIViewController kind of "added" to your existing UIViewController. So the UINavigationBar won't be there.

But you can add another UINavigationBar from xib and add a UIButton. But don't directly add the UIButton, if you do so it will automatically convert to UIBarButton making it bordered.

So first drag a normal UIButton to your UIView(not navigation bar). Change it to custom and add UIImage to it using Attribute Inspector. Then drag that image to your custom created UINavigationBar. It will work as I tried it just now. You can see the screen shot attached below.

enter image description here

like image 38
Javvadi Rajesh Avatar answered Sep 29 '22 11:09

Javvadi Rajesh