How can I add a middle button in a UINavigationController? - iphone

I have a UINavigationController that has its left and right button properties set. Instead of having the title in the middle, I want to put a button there. I'm not sure how I can achieve this.
CODE:
UIBarButtonItem* titleButton = [[UIBarButtonItem alloc]
initWithTitle:symbol
style:UIBarButtonItemStyleBordered
target:self
action:#selector(addToWatchlistButtonClicked:)];
self.navigationItem.titleView = titleButton;

Use titleView property of UINavigationItem.
self.navigationItem.titleView = button;

Related

multiple rightbar buttons in navigationbar in xcode4.2

I want to create three buttons in right side of Navigation bar.I am using storyboard for creating the UIView Controller.
In DetailViewController am Embedded a Navigation bar using storyboard and then creating three UIBarButtonItem programetically and then adding these in an array then assigning it to navigationitem.This is working fine.
UIBarButtonItem *Button1 = [[UIBarButtonItem alloc]initWithTitle:#"Button1" style:UIBarButtonItemStylePlain
target:self action:#selector(Button1Clicked:)] ;
UIBarButtonItem *Button2 = [[UIBarButtonItem alloc] initWithTitle:#"Button2" style:UIBarButtonItemStylePlain
target:self action:#selector(Button2Clicked:)] ;
UIBarButtonItem *Button3 = [[UIBarButtonItem alloc] initWithTitle:#"Button3" style:UIBarButtonItemStylePlain
target:self action:#selector(Button3Clicked::)] ;
self.navigationItem.rightBarButtonItems =
[NSArray arrayWithObjects:Button1,Button2,Button3, nil];
I have another viewcontroller this is a modalviewcontreoller. I am creating the view controller using storyboard and adding a navigationbar not a navigationcontroller.Then using the same method for adding buttons to navigtion bar, but not shown any buttons.
Plese anyone know how to solve this issue.?
If your viewController is in UINavigationController you can simply use
self.navigationItem ...
which works fine as I see. But if your viewController (your modal in this case) is not in UINavController, you have to access UINavigationItem in this way:
someNavigationBar.topItem ...
So, if you set an IBOutlet to your navigationBar, your code should look like this one:
UIBarButtonItem *Button1 = ...
UIBarButtonItem *Button2 = ...
UIBarButtonItem *Button3 = ...
yourNavigationBar.topItem.rightBarButtonItems = [NSArray arrayWithObjects:Button1, Button2, Button3, nil];
Please add UINavigationBar instead of UINavigationItem

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.

Subclassing UINavigationController to have extra icons for rightbarbuttonitem and leftbarbuttonitem

I'm looking at some other iPad apps that have the capability to add extra icons into the leftBarButtonItem and rightBarButtonItem for UINavigationController. Here is a picture of one taken from nngroup.com:
In order to do something like this, are they subclassing UINavigationController or adding a category for it? Or are they doing something different? I kind of just wanted the general approach of how to do something like this, but don't need all the code since I'm sure it's a lot of work. Thanks.
You can create a toolbar object and add the buttons to that toolbar and then set the custom view of the right bar button item or left bar button item as this toolbar.
A sample implementation is like this.
UIToolbar * toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0,0,160, 44.5)];
UIBarButtonItem *spacer = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
[buttons addObject:spacer];
[spacer release];
UIBarButtonItem * deleteButton = [[UIBarButtonItem alloc]
initWithTitle:#"Delete"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(deletePressed)];
// Button style is the default style
[buttons addObject:deleteButton];
[deleteButton release];
UIBarButtonItem * barItem = [[UIBarButtonItem alloc] initWithTitle:#"Logout" style:UIBarButtonItemStyleBordered
target:[Y3AppDelegate mainApplicationInstance] action:#selector(logout)];
[buttons addObject:barItem];
[barItem release];
// put the buttons in the toolbar and release them
[toolbar setItems:buttons animated:YES];
[buttons release];
// place the toolbar into the navigation bar as Right Button item
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
initWithCustomView:toolbar] autorelease];
[toolbar release];
One thing to note here is that you may have to change the tint color of the toolbar to match with that of the navigation bar. Hope this answer helps you.
You can hide Navigation bar and instead of that show an image of navigation bar and then place buttons on it :))

UIBarButtonItem doesn't highlight when tapped if in a Toolbar?

I need two buttons on the left side of a nav bar. The only way I've figured out how to do that is by first putting them into a UIToolbar and then setting the leftBarButtonItem to that.
If I do this it works as normal (you can see it highlight when tapped):
UIBarButtonItem* myBtn = [[UIBarButtonItem alloc] initWithTitle:#"Button" style:UIBarButtonItemStyleBordered target:self action:#selector(doSomething:)];
self.navigationItem.leftBarButtonItem = myBtn;
But if I do it like this, the button action still happens but there is no highlight (no visual feedback that you are tapping the button):
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:2];
UIBarButtonItem* myBtn = [[UIBarButtonItem alloc] initWithTitle:#"Button" style:UIBarButtonItemStyleBordered target:self action:#selector(doSomething:)];
UIBarButtonItem* myBtn2 = [[UIBarButtonItem alloc] initWithTitle:#"Button2" style:UIBarButtonItemStyleBordered target:self action:#selector(doSomethingElse:)];
[buttons addObject:myBtn];
[buttons addObject:myBtn2];
UIToolbar* toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 133, 44)];
[toolbar setItems:buttons animated:NO];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:toolbar];
Any idea why this causes the buttons to not highlight when touched?
I dont think the UIBarButtonItem object will be highlighted when touched up. Even for default back button in the navigation bar dosent highlight when touched up. It works in that way only . Not sure but you can try using UISegmentedControl with the single segment . It might create the highlighted illusion and would look like barbutton only.
In your selector function (e.g. doSomething), set the tintColor property of the button to your desired highlighted color if pressed, and the default if unpressed. And make sure the highlight color of your button is different than the default color so that you know that it is highlighted.
As per the Apple Docs, the function of showsTouchWhenHighlighted is:
A Boolean value that determines whether tapping the button causes it to glow.
but this is only for UIButton. I don't believe UIBarButtonItem has a highlight option.
Note that UIBarButtonItem inherits from UIBarItem which inherits from NSObject, not UIButton, so you can expect different behavior.

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;