First UIBarButton disappears when second one is added - ios

I am working on ios Bar Button icon where I have to display UIsegmented control with two UIBarButton icon on the right site but when I try to add second button using interface builder the first added button get disappears
This is how the view is added:
View->Navigation Bar->Navigation Item-> BarButton Item-> Button
Can anyone help me to find the possible solution

UIBarButtonItem *item1=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(firstSelector)];
UIBarButtonItem *item2=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(secondSelector)];
NSArray *items=[[NSArray alloc]initWithObjects:item1,item2, nil];
self.navigationItem.rightBarButtonItems=items;

Related

How to set the navigation bar button when user taps “More”on UIActivityViewController?

Whenever the user wants to select a new share method or action that isn't listed by default, by tapping the "More" button on the UIActivityViewController generated share sheet, a new view is displayed, As everybody knows.i need to change the bar button on that page
can anybody help me on adding our own bar button as a rightbarbutton item?
Try with this. if not you want this ,then explain a little bit more.
UIBarButtonItem *addButton = [[UIBarButtonItem alloc]initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(doneButtonPressed)];
self.navigationItem.rightBarButtonItem = addButton;

Add a button next to the back button

in iOS, is it possible to add a button next to the default back button (the one that is automatically created when you use the method pushViewController)
I've tried to use the methods setLeftBarButtonItems and setBackBarButtonItem but in vain - whenever I set the left bar button items, the default back button disappears.
Thanks for your help!
Just do
self.navigationItem.leftItemsSupplementBackButton = YES;
self.navigationItem.leftBarButtonItems = #[item1, item2];
I haven't tested the following code, but it should work as long as the backBarButtonItem has been initialised.
[[self navigationItem] setLeftBarButtonItems:[NSArray arrayWithObjects:[[self navigationItem] backBarButtonItem], [[UIBarButtonItem alloc] initWithTitle:#"Custom" style:UIBarButtonItemStylePlain target:self action:#selector(action:)], nil]];\
Essentially, you're setting the entire left bar button item array from scratch but providing the back button along with your own custom button.

navcontroller - Setting up Navigation Item as image

I would like to setup the navigation controller bar button item to be an image.
I have read the following - Add image to a navigationItem's title
I understand how to setup this as an image. I am actually looking to set this up as the setting cog, within the bar button item (like a cog icon). I have seen this before in other apps, I would like to know if there is a default style to initialise for this or normally if this is achieved by the image route as above.
If this is above, does anyone know where this (kind of) default image styled icon is available, or would I need to make one?
Have you tried using the initWithCustomView option of UIBarButtonItem? A sample code will look like the below line.
UIBarButtonItem *aButton = [[UIBarButtonItem alloc] initWithCustomView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"settings.png"]]];
Another way to do this is,
UIBarButtonItem *aButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"settings.png"] style:UIBarButtonItemStyleBordered target:self action:#selector(buttonTapped)];
If you use storyboard, you can directly:
Drag a Button in the navigation bar instead of a Bar Button Item. It will automatically create a Button Bar Item for you with a Button inside it.
Enter a title for the Button Bar Item
Set an image for the Button with a Custom type.
It should works.

Back, Edit and Add buttons in navigation bar of TableView with Storyboard on iOS

I'm facing some problems in implementing a tableview, with "Back", "Edit" and "Add" buttons on the navigation bar.
The tableview is reached by clicking on a row of another tableview, so the "Back" button is added automatically.
With the storyboard I've added the "Add" button to the navigation bar.
With code I've added the "Edit" button (I used code, since if I add the button with the storyboard, I don't know how to reproduce the "Edit" standard behavior...):
self.navigationItem.leftBarButtonItem = self.editButtonItem;
The problem is that, in this way, the "Edit" button hides the "Back" button on the navigation bar.
At this point, I've two questions:
Is it possible with storyboard to add a third button on the navigation bar?
In case I've to do this programmatically, I know that I can do this as follows:
UIButton *button = [UIButton buttonWithType: UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(width-90,6,50,30)];
[button setTitle:#"Edit" forState:UIControlStateNormal];
button.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
[self.navigationController.navigationBar addSubview:button];
But how can I implement via code the standard behavior of the "Edit" button? I mean, I click "Edit" and the button becomes "Done" and the rows become deletable...
Thanks in advance,
yassa
Incase anyone else should happen to stumble onto this question as well the solution is pretty easy. UINavigationItem has a property for rightItems wich is just an array of UIBarButtonItems. Put both an add button and an edit button into an array and assign it to rightItems and your done :-) And here is an example code snippet:
UITableViewController *table = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(insertNewObject:)];
NSArray *barButtons = [NSArray arrayWithObjects:table.editButtonItem, addButton, nil];
table.navigationItem.rightBarButtonItems = barButtons;
First, the Apple docs say 'you do not add subviews to a navigation bar directly'. Don't know if this is enough to get the app bounced from the store, but it's not considered "proper".
Second, you can add more than three buttons to a UINavigationItem in iOS 5 but not in iOS 4 or earlier.
Finally, I'd leave the edit button top right and back top left. That's where people expect them. If I wanted an add button (and are on iOS 5), I'd place it next to the edit button.
Sorry; no help on storyboards. Don't know anything about them.

Add another button next to the "back" button on the left of a UINavigationBar

I've tried this for hours but I still cannot solve it.
When using UINavigationController and push a new view controller on top, I got a free "back" button on the left of the navigation bar. I want another button just next to it (to show a popover menu). I wonder what is the correct way to do that. Or I have to hide the free back button and make the same one by myself? If that's the case, I also need to pop the current view controller when pressing my own back button, right?
Thanks for your help.
As stated by steipete in the comment to the question, this is possible starting from iOS 5. You can use
self.navigationItem.leftItemsSupplementBackButton = YES;
and then you just need to add an UIBarButtonItem as leftButton to get a second button after the back button
UIBarButtonItem *secondButton = [[UIBarButtonItem alloc] initWithTitle:#"Second" style:UIBarButtonItemStylePlain target:self action:#selector(yourAction)];
self.navigationItem.leftBarButtonItem = secondButton;
SWIFT 5 this worked for me. Thanks
self.navigationItem.leftItemsSupplementBackButton = true

Resources