Custom UIBarButtonItem - ios

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?
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?

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;

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.

This will update ALL back buttons and navigation bars throughout the app.
I have used the Following code to set Custom Navigation back button
UIImage *imgBack = [[UIImage imageNamed:#"nav_back.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(-6, 22, -6, 0)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:imgBack forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
and the Following code for Setting background image on navigation bar.
UIImage *navBackgroundImage = [UIImage imageNamed:#"nav_bar.png"];
[[UINavigationBar appearance] setBackgroundImage:navBackgroundImage forBarMetrics:UIBarMetricsDefault];

Try use [UIBarButtonItem initWithCustomView]
UIImage *menuButtonImage = [UIImage imageNamed:#"list.png"];// set your image Name here
UIButton *btnToggle = [UIButton buttonWithType:UIButtonTypeCustom];
[btnToggle setImage:menuButtonImage forState:UIControlStateNormal];
btnToggle.frame = CGRectMake(0, 0, menuButtonImage.size.width, menuButtonImage.size.height);
UIBarButtonItem *menuBarButton = [[UIBarButtonItem alloc] initWithCustomView:btnToggle];
self.navigationItem.leftBarButtonItem = menuBarButton;

I personally like FlatUIKit's approach by just flattening out the entire UI through UIAppearance
Essentially, you'd do something like:
UIImage *backButtonPortraitImage = [UIImage backButtonImageWithColor:color barMetrics:UIBarMetricsDefault cornerRadius:cornerRadius];
UIImage *highlightedBackButtonPortraitImage = [UIImage backButtonImageWithColor:highlightedColor barMetrics:UIBarMetricsDefault cornerRadius:cornerRadius];
[appearance setBackButtonBackgroundImage:backButtonPortraitImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[appearance setBackButtonBackgroundImage:highlightedBackButtonPortraitImage forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
UIImage *buttonImageNormal = [UIImage imageWithColor:color cornerRadius:cornerRadius];
UIImage *buttonImageHightlighted = [UIImage imageWithColor:highlightedColor cornerRadius:cornerRadius];
[appearance setBackgroundImage:buttonImageNormal forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[appearance setBackgroundImage:buttonImageHightlighted forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];

Try this code..
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *btnImage = [UIImage imageNamed:#"bg"];
[button setImage:btnImage forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, btnImage.size.width, btnImage.size.height);
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
UIImage *icon = [UIImage imageNamed:#"icon"]
UIImageView *iconView = [[UIImageView alloc] initWithImage:icon];
iconView.frame = CGRectMake(0, 0, icon.size.width, icon.size.height);
[button addSubview:iconView];
iconView..userInteractionEnabled=NO;
[iconView centerBothDirections];
[iconView release];
UIBarButtonItem *barbtn = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];
self.navigationItem.leftBarButtonItem =barbtn

Related

UIBarButtonItem appearance how to remove text?

I am customizing my nag bar button in iOS7 and I am using the following code:
// Customizing the NavBar Buttons
UIImage * btHome = [[UIImage imageNamed:#"Home.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 3, 0, 3)];
[[UIBarButtonItem appearance] setBackgroundImage:btHome forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
It is working, but the view shows my home button with the button text over it, I do not want the text to show.
What else do I need to do to remove the text?
What I have is shown here:
Try to set
1) text color on a bar button item through appearance
2) use setImage: instead of setBackgroundImage: and do not use resizable image
Try with this code. May be it will help you.
UIImage *faceImage = [UIImage imageNamed:#"back_arrow.png"];
UIButton *yourbutton = [UIButton buttonWithType:UIButtonTypeCustom];
yourbutton.bounds = CGRectMake( 10, 0, faceImage.size.width, faceImage.size.height );
[yourbutton addTarget:self action:#selector(handleBack:) forControlEvents:UIControlEventTouchUpInside];
[yourbutton setImage:faceImage forState:UIControlStateNormal];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:yourbutton];
self.navigationItem.leftBarButtonItem = backButton;
Sets the back button title offset for given bar metrics
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60) forBarMetrics:UIBarMetricsDefault];

iOS Change background image of navigation bar cancel button

I have a UIImagePickerController that pulls from the camera roll.
In the navigation bar, there is a default UIBarButtonItem to cancel selecting a photo, but it does not match my Application at all!
How can I change its appearance or even hide it and then put my own button in its place in the nav bar?
This is easily done with the Back button like so:
// Change the appearance of back button
UIImage *backButtonImage = [[UIImage imageNamed:#"back_button.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(20, 10, 0, 0)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
There does not seem to be a setCancelButtonBackgroundImage method for UIBarButtonItem.
How about this one:
UIImage * cancelButtonImage = [UIImage imageNamed:#"cancel_button.png"];
UIButton * cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
[cancelButton setFrame:(CGRect){CGPointZero, hamburgerImage.size}];
[cancelButton setImage:cancelButtonImage forState:UIControlStateNormal];
[cancelButton addTarget:self
action:#selector(cancel:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem * cancelButtonItem = [[UIBarButtonItem alloc] initWithCustomView:cancelButton];
[self.navigationItem setLeftBarButtonItem:cancelButtonItem];

How to place UINavigationBarButton in custom location

My current navigation bar looks like this:
As you can see I need to move it to the right. Not sure that I'm setting this up correctly, any suggestions?
Here is the code:
-(void)customizeNavigationBar{
//*** Nav bar Background ***
//Image being used
UIImage *backgroundImage = [UIImage imageNamed:#"navBarBackground"];
//Set navigation bar image
[self.navigationController.navigationBar setBackgroundImage:backgroundImage forBarMetrics:UIBarMetricsDefault];
//*** Search button**
//Get Image
UIImage* searchImage = [UIImage imageNamed:#"navSearchButton"];
//Set frame
CGRect frame = CGRectMake(0, 0, searchImage.size.width, searchImage.size.height);
//Create button
UIButton *searchButton = [[UIButton alloc] initWithFrame:frame];
//Set background images (selected and not)
[searchButton setBackgroundImage:searchImage forState:UIControlStateNormal];
[searchButton setBackgroundImage:[UIImage imageNamed:#"navSearchButtonSelected"] forState:UIControlStateSelected];
//Add action
[searchButton addTarget:self action:#selector(search)
forControlEvents:UIControlEventTouchUpInside];
//Set highlight on
[searchButton setShowsTouchWhenHighlighted:YES];
//Create nav buttons
UIBarButtonItem *navSearchBtn =[[UIBarButtonItem alloc] initWithCustomView:searchButton];
//Add to nav bar
[self.navigationItem setLeftBarButtonItem:navSearchBtn];
}
Use setImage:forState: for setting background image to UIButton (instead of setBackgroundImage:)
Set UIButton's frame a bit larger (size.width)

How to have iOS set a gradient on UIBarButtonItem image?

just wondering, I customised the UIBarButtonItems the following way
+ (UIBarButtonItem *)createBarButtonItemWithTitle:(NSString *)t target:(id)tgt action:(SEL)a
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *buttonImage = [[UIImage imageNamed:#"blabla.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 10, 0, 10)];
CGRect buttonFrame = [button frame];
buttonFrame.size.width = 35;
buttonFrame.size.height = buttonImage.size.height;
[button setFrame:buttonFrame];
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
[button addTarget:tgt action:a forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
return buttonItem ;
}
But now, I don't have the shading/glossy effect on the button background image....
Is there a way to have it done automatically, or I have to do it programatically ?
EDIT:
To totally customize the bar button in such a way you'd need to provide something other than the flat green image in "blabla.png".
A different way to handle this would be to create a bordered style button with your image. Something like:
UIBarButtonItem* barButton = [UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"blabla.png"] style:UIBarButtonItemStyleBordered target:tgt selector:a];
The UIBarButtonItemStyleBordered style gives it the 3D raised button-y look. You can tint its appearance to match your bar if desired.

Can set background image for UIBarButtonItem in nav bar, but not bottom toolbar

So this works when adding the item to the nav bar, but when i add it to the toolbar set in the bottom bar via interface builder, the background image doesn't show up.
UIBarButtonItem *resetButtonItem = [[UIBarButtonItem alloc]initWithTitle:#"Reset" style:UIBarButtonItemStylePlain target:self action:#selector(resetCriteria:)];
UIImage *img = [UIImage imageNamed:#"someImage.png"];
img = [img stretchableImageWithLeftCapWidth:5 topCapHeight:20];
[resetButtonItem setBackgroundImage:img forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
self.toolbarItems = [NSArray arrayWithObjects: resetButtonItem, nil];
Not only does the background not appear, none of the other behaviors work as well (but they work fine when adding these barbutton items to the nav bar)
You should try the following method to do the same.
Setting custom view in UIBarButtonItem.
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(0, 0, 30, 30);
[btn setImage:[UIImage imageNamed:#"someImage.png"] forState:UIControlStateNormal];
[btn addTarget:self action:#selector(resetCriteria:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *Item = [[UIBarButtonItem alloc] initWithCustomView:btn];
self.toolbarItems = [NSArray arrayWithObject:Item];
[Item release];
We can set any view in UIBarButtonItem with CustomView init method.
We could set the ImageView. But ImageView doesnot handle UIControl Events.

Resources