How to add image(s) to UIToolBar? - iphone

I'd like to add an image to a UIToolBar that a user cannot interact with. It would essentially be just a non-interactive indicator, like a badge.
Is this possible?
If so, how?

Create an UIBarButtonItem with an Image and add it:
Example:
UIBarButtonItem* item = [[UIBarButtonItem alloc] initWithImage:yourImage style:UIBarButtonItemStyleBordered target:nil action:nil];
If you don't want the button appearance, try to use:
UIBarButtonItem* item = [UIBarButtonItem initWithCustomView:yourImageView];

Related

UIBarButtonItem with no border

How can I create an UIBarButtonItem without any border, but just a label inside?
EDIT
I tried something like this with no luck:
UIBarButtonItem* langButton = [[UIBarButtonItem alloc] initWithTitle:#"EN"
style:UIBarButtonItemStylePlain target:self action:#selector(changeLang)];
self.navigationItem.leftBarButtonItem = langButton;
[langButton release];
Do the same as the following guy:
https://stackoverflow.com/a/6817554/59198
But change it so instead of an image button, you'll make a standard textual button.

need help about add navigationItem

As you see in the images, the top picture is created automatically when navigation controller is pushed
If I try to create one like this it will appear like the bottom picture.
How can I programmatically create a Back Button like the top picture?
Here is my code to create the Done button
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(backtohome)];
UIBarButtonItem *theBackButton = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStylePlain target:self action:nil];
self.navigationItem.backBarButtonItem = theBackButton;
[theBackButton release];

Set the position of a UIBarButtonItem Programmatically

How would I be able to set the position of a UIBarButtonItem? Like, I would like to set it to be on either the very right of a UIToolbar or the very left depending on a state.
Thank you.
You do not set directly the position of a UIBarButtonItem in an UIToolbar. Instead you defined the items' order and put flexible space on the left or on the right.
What you can do is:
Create the UIBarButtonItem you want to place (button 1).
Create an UIBarButtonItem of type UIBarButtonSystemItemFlexibleSpace (button 2).
If you want to put the button on the left, create an array with (button 1) and (button 2) and pass it to the UIToolbar by using the setItems:animated: method.
If you want to put the button on the right, create an array with (button 2) and (button 1) and pass it to the UIToolbar by using the setItems:animated: method.
I hope it helps you...
UIToolbar* keyboardDoneButtonView = [[UIToolbar alloc] init];
[keyboardDoneButtonView sizeToFit];
UIBarButtonItem* PrevButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:105 target:nil action:nil]; //<
UIBarButtonItem* NextButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:106 target:nil action:nil]; //>
UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleBordered target:self action:#selector(doneClicked:)];
UIBarButtonItem* flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *fake = [[UIBarButtonItem alloc] initWithTitle:#"" style:UIBarButtonItemStylePlain target:nil action:nil] ;
[keyboardDoneButtonView setItems:[NSArray arrayWithObjects: PrevButton,fake, NextButton,fake,flexSpace,fake,doneButton,nil] animated:YES];
Link explains Bar Button Item Methods and Arguments
https://developer.apple.com/library..... Use Fake Item to get exact pinch location on Button...

How to use an icon as back button for UINaviationController

Does anyone know how to use an icon instead of text for the UINavigationController back button?
alt text http://www.img-hosting.de/bilder/24505iPhoneSimulator2png
Try this:
UIBarItem *backBar = [[UIBarButtonItem alloc] initWithImage:yourImage style:UIBarButtonItemStyleDone target:nil action:nil];
self.navigationItem.backBarButtonItem = backBar;
[backBar release];
you can also use initWithCustomView if you need something more elaborate

Programmatically changing the identifier property of UIBarButtonItem

Through Interface Builder I have the ability to change the Identifier of a UIBarButtonItem to something like "Add" (or "Undo", "Redo" etc...). This gives my button a nice "+" image.
How can I set this programatically? The UIBarButtonItem does not accept a "setIdentifier" message.
Once constructed, a UIBarButtonItem's "Identifier" can not be modified. However, the UI can be changed by replacing the button with a programmatically constructed variant. For example:
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:#selector(doAddAction:)];
If you want one button style: bordered and identifier: camera use
UIBarButtonItem *btn;
btn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:#selector(shotAction:)];
btn.style=UIBarButtonItemStyleBordered;
Once the UIBarButtonItem is created, there is no way to change the identifier. However, you can create a new UIBarButtonItem to replace the old UIBarButtonItem
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction
target:self
action:#selector(buttonClickedAction:)];
self.navigationItem.rightBarButtonItem = barButton;