UIBarButtonItem not shown the same way in UIToolBar and UINavigationBar - ios

I need to use a simple UIBarButtonItem in a UIToolBar.
I used this code to add the button with my custom image to the navigation bar:
UIBarButtonItem *cloneButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"image_sheep.png"] style:UIBarButtonItemStylePlain target:self action:#selector(clone)];
NSArray *rightItems = [NSArray arrayWithObject:cloneButton];
self.navigationItem.rightBarButtonItems = rightItems;
The result is what I want and it looks like this
navigation bar http://img207.imageshack.us/img207/3383/navigationbara.jpg
When doing the same thing in a UIToolBar that I'm adding to a UITableViewCell's contentView
UIBarButtonItem *cloneButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"image_sheep.png"] style:UIBarButtonItemStylePlain target:self action:#selector(clone)];
UIBarButtonItem *leftSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
toolbar.items = [NSArray arrayWithObjects:leftSpace, cloneButton, nil];
The problem is that I get something like this:
toolbar http://img209.imageshack.us/img209/1374/toolbary.jpg
This is surely due to the fact that UINavigationBar and UIToolBar are not drawn the same way...
Can someone please point out how to resolve this problem?

On your UIToolbar use UIBarButtonItemStyleBordered instead of UIBarButtonItemStylePlain.
UINavigationBar will not draw the button the same without the button look.

That's how UIToolbar is supposed to work. Per the documentation:
Toolbar images that represent normal and highlighted states of an item derive from the image you set using the inherited image property from the UIBarItem class. For example, the image is converted to white and then bevelled by adding a shadow for the normal state.
That said, you might be able to get the result you want by creating a UIBarButtonItem with a custom view instead of an image. Like so:
UIImage *sheepImage = [UIImage imageNamed:#"image_sheep.png"];
UIButton *sheepButton = [UIButton buttonWithType:UIButtonTypeCustom];
[sheepButton setImage:sheepImage forState:UIControlStateNormal];
[sheepButton addTarget:self action:#selector(clone) forControlEvents:UIControlEventTouchUpInside];
[sheepButton setShowsTouchWhenHighlighted:YES];
[sheepButton sizeToFit];
UIBarButtonItem *cloneButton = [[UIBarButtonItem alloc] initWithCustomView:sheepButton];
I haven't tested this, so I don't know if it will work.

I cannot see the images posted in the question anymore. But I am pretty sure that we're trying to mimic the UIBarButtonItemStylePlain of the UIToolBar in a UINavigationBar.
Benzado is pretty much spot on except for this line:
[sheepButton setImage:sheepImage forState:UIControlStateNormal];
This puts the image in front of the touch indicator. The UIToolBar shows the touch in front of the image. So to mimic the UIToolBar style in a UINavigationBar:
UIImage *sheepImage = [UIImage imageNamed:#"image_sheep.png"];
UIButton *sheepButton = [UIButton buttonWithType:UIButtonTypeCustom];
[sheepButton setBackgroundImage:sheepImage forState:UIControlStateNormal];
[sheepButton addTarget:self action:#selector(clone) forControlEvents:UIControlEventTouchUpInside];
[sheepButton setShowsTouchWhenHighlighted:YES];
[sheepButton sizeToFit];
UIBarButtonItem *cloneButton = [[UIBarButtonItem alloc] initWithCustomView:sheepButton];

Related

Make UIBarButtonItem transparent iOS 7/8

i know there are a lot of question/answers about this topic, but can't find a fitting answer.
I building a UIBar programmatically. My own buttons (png with transparancy) are having a back fill instead of showing the background trough.
The system UIBarButton's are working perfect, but not my own.
works:
UIBarButtonItem *exportButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAction
target:self
action:#selector(tapExportButton:)];
not works:
UIBarButtonItem *favButton = [[UIBarButtonItem alloc]
initWithImage:[UIImage
imageNamed:#"FavoriteStar"]
style:UIBarButtonItemStylePlain
target:self
action:#selector(toggleFavorite:)];
the inner part of the png is transparent in my gfx editor
the inner part is black when i use it as a UIBarButtonItem, but not the background picture...
What can i do to make it transparent?
Regards,
Jeroen Wolff
I am revising your code for better understanding
UIBarButtonItem *favButton = [[UIBarButtonItem alloc]
initWithImage:[UIImage
imageNamed:#"FavoriteStar"]
style:UIBarButtonItemStylePlain
target:self
action:#selector(toggleFavorite:)];
below are some points that needs to be taken care of:
1. Your navigation bar should be transparent.
2. Use a PNG file (for better quality) with its transparent background.
3. You can also use the below code for reference
UIButton *btnBack=[UIButton buttonWithType:UIButtonTypeCustom];
btnBack.frame=CGRectMake(0, 29.5, 38, 25);
[btnBack setBackgroundImage:[UIImage imageNamed:#"btn_back.png"] forState:UIControlStateNormal];
[btnBack setBackgroundImage:[UIImage imageNamed:#"btn_backSelected.png"] forState:UIControlStateHighlighted];
[btnBack addTarget:self action:#selector(btnbackClicked:) forControlEvents:UIControlEventTouchDown];
UIBarButtonItem *barbutton=[[UIBarButtonItem alloc]initWithCustomView:btnBack];
[barbutton setStyle:UIBarButtonItemStyleBordered];
self.navigationItem.leftBarButtonItem=barbutton;
I have used a custom button as bar button.
I am really want to hear from you. Please let me know if it works.

Can't set image to UINavigationBar's rightTabBarItem

I'm trying to set image to rightBarButtonItem.
I tried to do this 2 ways:
/* first way */
UIButton *button1 = [[UIButton alloc] init];
button1.frame=CGRectMake(0,0,105,30);
[button1 setBackgroundImage:[UIImage imageNamed: #"image.png"] forState:UIControlStateNormal];
[button1 addTarget:self action:#selector(rightBarButtonItemTapped) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button1];
/* second way */
UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"image"]
style:UIBarButtonItemStyleBordered
target:self
action:#selector(rightBarButtonItemTapped)];
P.S. When I tap right side of navigation bar I see that rightBarButtonItemTapped method invoked.
You can set an image to a bar button using the attribute inspector.

Any idea why the custom nav bar back button image isn't displaying with this code?

Any idea why the custom nav bar back button image isn't displaying with this code?
instead of showing the uiimage it's showing the "< Back" default iOS button in text only
[self.navigationController setNavigationBarHidden:NO animated:NO];
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"freccia.png"] landscapeImagePhone:nil style:UIBarButtonItemStylePlain target:nil action:#selector(backBtnPress)];
could it be this parameter? UIBarButtonItemStylePlain
thanks
use this code:
UIImage *imageBack = [UIImage imageNamed:#"yourImage.png"];
UIButton *btnBack = [UIButton buttonWithType:UIButtonTypeCustom];
btnBack.bounds = CGRectMake(0, 0, imageBack.size.width, imageBack.size.height);
[btnBack setBackgroundImage:imageBack forState:UIControlStateNormal];
[btnBack addTarget:self action:#selector(back) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButtonBack = [[UIBarButtonItem alloc] initWithCustomView:btnBack];
self.navigationItem.hidesBackButton = YES;
self.navigationItem.leftBarButtonItem = barButtonBack;
-(void)back {
[self.navigationController popViewControllerAnimated:YES];
}
You usually set the back button in the parent view controller. However, it might just be easier to use leftBarButtonItem instead of backBarButtonItem.
you can solve this problem by two ways
1. use leftBarButtonItem instead of backBarButtonItem
2. try the below code
self.navigationItem.backBarButtonItem =[[UIBarButtonItem alloc] initWithTitle:#" " style:UIBarButtonItemStylePlain target:nil action:nil];

How can I remove default button?

How can I remove dafult button to place my custom button in navbar? Problem at the moment is that my custom button is over default button.
Please take a look at the screenshot and it will be more clear.
I'm getting this with following code:
- (void)viewDidLoad
{
UIImage *menuImage = [UIImage imageNamed:#"barMenuButton.png"];
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithImage:menuImage style:UIBarButtonItemStylePlain target:self action:#selector(ShowLeftMenu:)];
[self.navigationItem setRightBarButtonItem:addButton];
}
This will fix your problem:
UIImage *menuImage = [UIImage imageNamed:#"barMenuButton.png"];
UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 100, 40);
[button setImage:menuImage forState:UIControlStateNormal];
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithCustomView:button];
[addButton setAction:#selector(ShowLeftMenu:)];
[self.navigationItem setRightBarButtonItem:addButton];
You'll have to create a custom view and add that to the bar button item, as described in this article. This article also provides some details about creating custom UIButtons.

How to make leftBarButtonItem looking like backBarButtonItem?

The default solvings are not appropriate:
to change the previous ViewController title - I need to make my own function controlling the button touches up
to make a leftBarButtonItem and hide backBarButtonItem - leftBarButtonItem doesn't look like a default backBarButtonItem.
Here's an example to create a custom leftBarButtonItem:
UIImage *buttonImage = [UIImage imageNamed:#"backbutton.png"];
UIButton *aButton = [UIbutton buttonWithType:UIButtonTypeCustom];
[aButton setImage:buttonImage forState:UIControlStateNormal];
aButton.frame = CGRectMake(0.0,0.0,buttonImage.size.width,buttonImage.size.height);
[aButton addTarget:self action:#selector(aSelector) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:aButton];
self.navigationItem.leftButtonItem = backButton;
Hope it helps...
Edit:
Try this...
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"yourimage.jpg"] style:UIBarButtonItemStyleBordered target:self action:#selector(aSelector)];
self.navigationItem.leftBarButtonItem = backButton;
I don't remember what type of button is the backbutton, try to change the default style to other. Good luck!
Just create a custom barbuttonitem and customize it with a picture similar to the back button item.
You can get its text from the view controllers in the navigation stack.
Then handle the taps to implement the desired effect
You can assign your custom views as the bar buttons to your navigation controller, like this:
UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithCustomView:yourCustomizedControl];
[yourCustomizedControl release];
self.navigationItem.leftBarButtonItem = customItem;
[customItem release];
Check this Apple documentation for a more detailed explanation, if you want.
Here is a hack to add a back-like button:
UINavigationItem* backNavigationItem = [[UINavigationItem alloc] initWithTitle:#"Back"];
[_navigationBar pushNavigationItem:backNavigationItem animated:NO];
[backNavigationItem release];
You can add more items (such like title, right button) this way:
UINavigationItem *navigationItem = [[UINavigationItem alloc] initWithTitle:#"Title"];
navigationItem.rightBarButtonItem = // Some UIBarButtonItem
[_navigationBar pushNavigationItem:navigationItem animated:NO];
[navigationItem release];

Resources