iPhone Add Item/Row Button in Navigation Bar - iphone

Is there a standard way to implement a button like the one below in a navigation bar? If not I can, of course, just use an image or maybe a custom view but as it's used in quite a few apps I thought there may be a method I'm missing.

UIBarButtonItem *barBtnItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(add:)];
self.navigationItem.rightBarButtonItem=barBtnItem;
[barBtnItem release];
this code use for creating + button.

That's one of the standard UIBarButtonSystemItem system icons (UIBarButtonSystemItemAdd) - take a look at the initWithBarButtonSystemItem:target:action: method in the UIBarButtonItem Class Reference.

Related

add multiple UIBarButtonItems to UINavigationItem in Storyboard?

It is easy to add a single UIBarButton item as you can see here in my Storyboard. But I want to add several and it seems I cannot using the Storyboard?
Which is annoying because then I lose the whole nice overview of the Storyboard and it's Segue's.
Is this the case? First project using Storyboards so I figured I might be missing something.
In XCode 4.6 (at least), you can drag a plain view onto the navigation bar (on whichever side you want multiple buttons), then drag a toolbar on top of that. Once you've done that, you can place multiple bar button items on the toolbar, and wire them up to actions / segues, etc, as normal. Make sure you have the view and toolbar both set up for default (transparent) background color.
In Xcode 7 you can finally just drag multiple UIBarButtonItems to the UINavigationItem directly in Interface Builder.
See WWDC 2015 Session 407 video titled:
Implementing UI Designs in Interface Builder
https://developer.apple.com/videos/wwdc/2015/?id=407
Try this
-(void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *editBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:#selector(editAction)];
UIBarButtonItem *saveBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:#selector(saveAction)];
self.navigationItem.rightBarButtonItems = [[NSArray alloc] initWithObjects:editBarButtonItem, saveBarButtonItem, nil];
}
We also need to implement the methods editAction and saveAction.
-(void)editAction
{
NSLog(#"edit button clicked");
}
-(void)saveAction
{
NSLog(#"save button clicked");
}

Making a dynamic NavigationBar

I want a UINavigationBar whose contents change in response to other events in the app. Most immediately, I'd like to have the buttons on the bar loaded dynamically in response to other variables in the program. But in the more general case I'd like to be able to change the contents of the UINavigationBar on the fly, while the program is running.
The hurdle I'm running into is that the UINavigationBar loads its contents when its first displayed and there doesn't seem to be a method to make it alter them after that. What's the best workaround?
This is fairly easy to do. The best way is to pre-load all of the options for the different objects you want in your navBar so that you need only switch them in and out based on the user's input, but you can load them on the fly. When the user does an action which you want to change the navBar, simply add:
self.navigationItem.rightBarButtonItem = nil;
UIBarButtonItem * newButton = [[UIBarButtonItem alloc] initWithTitle:#"What you want to call it" style:UIBarButtonItemStyleBordered target:self action:#selector(whatYouWantTheButtonToCall)];
self.navigationItem.rightBarButtonItem = newButton;
[newButton release];
For other options then a button, you can look here. Hope that helps!
OK, I found one way:
After editing a navigation bar item, you can call
[self loadView]
...to prompt a reload.
That works, but it's a bit blech.

iPhone/iPad: how to change location of home or cancel or any buttons in UINavigationController (with or without InterfaceBuilder)

My question is fairly straightforward and simple.
I have seen code that implements a button (such as a cancel button) that can be added to a UINavigationController. Code looks like this:
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc]
initWithImage: [UIImage imageNamed:#"home.png"]
style: UIBarButtonItemStylePlain target: self
action: #selector(cancel:)];
self.navigationItem.rightBarButtonItem = cancelButton;
[cancelButton release];
What I'd like to do is to place these buttons in a custom location in the navigationItem...not in the default rightBarButtonItem or leftBarButtonItem location, but controllable perhaps using x and y co-ordinates...is this even possible? It seems like a simple request, but google searching and documentation reviewing is driving me a little nuts :)
I would appreciate any assistance with this issue.
thanks again,
Edward
I agree with 'Hack Saw' in the case of it being against the Apple 'Human Interface Guide' and thus not recommended...However... If you want to, you can draw additional views into the navigation bar's title view [self.navigationContoller.navigationBar.topItem setTitleView]. Hope that helps!
Here is link to reference: UINavigationItem
They don't make that easy because it would break user expectation. I recommend against it. If you have a serious need for it, you'll probably need to make your own navbar.

Is it possible to change UIBarButtonItem title and style in runtime?

In one project, I hope to change UIBarButtonItem and style in runtime
editBarItemButton links to a UIBarButtonItem which original status are
style:UIBarButtonItemStyleBordered
title:Edit
if I press the bar item button, it will execute the codes below:
[editBarItemButton setStyle: UIBarButtonItemStyleDone];
[editTarBarItemButton setTitle:#"Done" ];
but neither the style nor title has changed.
Welcome any comment
Thanks
interdev
You cannot change the style of a UIBarButtonItem after it has already been initialized.
Old thread, but I am successful in changing the title for a UIBarButton item in a Navigation controller using the following:
[[[self navigationItem] rightBarButtonItem] setTitle:#"List"];
Just remove the old buttons
Create new buttons with the styles you want
Set the new buttons
What you want to do is something like this (that preserves localization for you app as well):
- (void)setBarButtonTo:(int)type
{
UIBarButtonItem *add = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:type target:self action:nil];
[self.navigationItem setRightBarButtonItem:add];
}
Where type is one of the system supplied, as UIBarButtonSystemItemDone or UIBarButtonSystemItemEdit. Hope this helps. :)
PS: I'm aware it's an old question, but it lacked a proper answer.

How to set the button created in the navigation bar as custom in iphone?

I am new to iphone development.I have created a UIBarButtonItem on the navigation bar.I want to set the proper to custom. There is no property as custom for "style:" attribute.Please help me out.Thanks.
leftbutton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"LEFT.png"] style:UIBarButtonItemStylePlain target:self action:#selector(leftbutton)];
self.navigationItem.leftBarButtonItem = leftbutton;
[self.navigationItem]
[leftbutton release];
If you just want to display a .png file inside the usual button frame from Apple, then your code is ok. If your png file is not displaying, just check if it's valid.
If you want to make your own button, you can use -(id)initWithCustomView:(UIView *)customView
Check this post for an interesting sample code :
Custom Bar Button Sample Code
You can create item with - (id)initWithCustomView:(UIView *)customView method with whatever custom view you want.
UIBarButtonItem has 4 initializers which cover all you will ever need:
-initWithBarButtonSystemItem:target:action: for standard (built-in items);
-initWithTitle:style:target:action: for buttons with title and no image;
-initWithImage:style:target:action: for buttons with image and no title;
-initWithCustomView: for any UIView subclass, Apple's or your own.
The style property makes sense only for standard buttons.