Hide the rightBarButtonItem of a navigation controller - ios

Does anyone know how to hide a rightBarButtonItem of a UINavigationController? In my application, I have an edit button as a rightBarButtonItem of a UINavigationController. I want to hide this ? UIBarButton` when some operations are done.

To Hide the right button: self.navigationItem.rightBarButtonItem = nil;
Now, to show it:
If you setup the right button in your view controller by assigning it to self.editButtonItem then simply assign it again in order to show it:
self.navigationItem.rightBarButtonItem = self.editButtonItem;
If you setup the right button in your view controller by allocating and initing a UIBarButtonItem, then simply keep a reference to the UIBarButtonItem in your view controller, and assign it again when you need to show it.

Try
self.navigationItem.rightBarButtonItem = nil;
When you want it back though you will have to instanciate a button i.e.
UIBarButtonItem *rightBarButton =
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch
target:self
action:#selector(searchBar:)];
self.navigationItem.rightBarButtonItem = rightBarButton;
[rightBarButton release];

If you need to hide/show the button based on some condition, try this:
if (condition) {
self.navigationItem.rightBarButtonItem.title = #"";
self.navigationItem.rightBarButtonItem.enabled = NO;
} else {
self.navigationItem.rightBarButtonItem.title = #"my button title";
self.navigationItem.rightBarButtonItem.enabled = YES;
}
This way you don't have to save a reference to the button in a property or worry about wiring up the action on a new button.

Related

Remove back bar button title from container view

I need empty back button title, so using category method as:
-(void)removeBackButtonTitle
{
UIBarButtonItem *newBackButton =
[[UIBarButtonItem alloc] initWithTitle:#""
style:UIBarButtonItemStylePlain
target:nil
action:nil];
[[self navigationItem] setBackBarButtonItem:newBackButton];
}
But I have a situation where I need to take Container View and some viewcontroller as child view controller and its view as a subview. Now upon navigation from this view, back button title is not setting to empty.
Simply add the following code to your viewDidLoad/viewWillAppear..
self.navigationController.navigationBar.topItem.title = #"";
This will remove Back text from that Back button. Also here you can set your own title as you desire.
#preetam .. for your case, every view controller has its lifecycle methods viewDidLoad, viewWillAppear.
Also, you are using Default navigation bar/Custom NavigationBar as you shown in screenshot.
If you're using Default, then your answer is given above. otherwise, you can remove the Back text in Storyboard itself.
Hope it helps..
Try this code and set setTitle to blank #"":
[self.navigationItem.backBarButtonItem setTitle:#""]; //make Title for backBarbutton to blank
It works for you
You need to add following lines of code in the (previous) view controllers where you don't want to display back button title.
- (void)viewWillAppear:(BOOL)animated {
self.title = #"Your Title";
}
- (void)viewWillDisappear:(BOOL)animated {
self.title = #"";
}
For Swift 3 you can use this:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let backItem = UIBarButtonItem()
backItem.title = ""
navigationItem.backBarButtonItem = backItem}
Try below line of code.
UIBarButtonItem *newBackButton =
[[UIBarButtonItem alloc] initWithTitle:#""
style:UIBarButtonItemStylePlain
target:nil
action:nil];
self.navigationItem.leftBarButtonItems =[[NSArray alloc] initWithObjects:newBackButton, nil];
self.navigationItem.leftItemsSupplementBackButton = YES;

iOS action of navigation bar back button not working [duplicate]

Trying to customize my back button in a drilldown navigation controller.
On my one view controller I have an Add button where the code programatically generates a new UIViewController:
- (void)add:(id)sender
{
MyAddViewController *addController = [[MyAddViewController alloc] initWithNibName:#"MyAddViewController" bundle:nil];
[self.navigationController pushViewController:addController animated:YES];
[addController release];
}
This works and when I click the add button it drills down into the new view. Inside the viewDidLoad method of MyAddViewController.m I have:
self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil] autorelease];
But this isn't working. The back button in the navigation controller remains the title of the previous view's controller on the stack. It seems that line does nothing. Did I miss something?
Thanks
self.navigationItem.backBarButtonItem is for the back button that appears on the view pushed by the view controller. So you need to move that line to the previous view controller.
This will only work on each child after the viewController that has self.navigationItem.backBarButtonItem.
You're confusing the backBarButtonItem and the leftBarButtonItem. From the UINavigationItem docs on backBarButtonItem:
When this item is the back item of the
navigation bar—when it is the next
item below the top item—it may be
represented as a back button on the
navigation bar. Use this property to
specify the back button. The target
and action of the back bar button item
you set should be nil. The default
value is a bar button item displaying
the navigation item’s title.
So, if you were to change:
self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil] autorelease];
To:
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil] autorelease];
I believe you would get the desired effect.
You can't replace the backBarButtonItem, but you can use the leftBarButtonItem to override it. But to get the new button to perform operate the same as the back button, you do need to set the target and action of the new button something like:
- (void)dismissMyView {
[self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]
initWithTitle:#"Quit" style: UIBarButtonItemStyleBordered
target:self action:#selector(dismissMyView)];
}
If ViewController A push ViewController B meanwhile we want to set the back bar button tittle, we should set "self.navigationItem.backBarButtonItem = ..".if it was set in ViewController B, it will not work as we want.

iPhone - Add "Add" Button when Edit button is selected in UITableView

i have a table view in my application which shows some items. when i click on one item, a new table view appears (with navigation controller: push). So at the top of the Table view there is now the navigationcontroller with the automatic "back" arrow to get back. i have the "edit" button enabled on the right side.
Now i want when i tap on the edit button, the Back button should disappear and a "+" add button should be there instead of the back button. Is this possible?
Or it is possible to get the Edit and Add button on the screen at the same time?
thanks
This is easy enough. Override the setEditing:animated: method of your view controller. This is called when the Edit/Done button is toggled (assuming you are using the standard editButtonItem from UIViewController).
In this method you create an "add" button and make it the left bar button item. This will hide the back button. Remove the "add" button and the back button will reappear.
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
if (editing) {
// Add the + button
UIBarButtonItem *addBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(addAction:)];
self.navigationItem.leftBarButtonItem = addBtn;
} else {
// remove the + button
self.navigationItem.leftBarButtonItem = nil;
}
}
Yep, this is the approach that I used:
Have a property for both the back button and the add button and set it in viewDidLoad:
self.backButton = self.navigationItem.leftBarButtonItem;
self.addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(addPressed:)];
Now you just have to swap the buttons and update the TableView state accordingly when 'Edit' is pressed. Here i also change the 'Edit' button to 'Done':
- (IBAction)editBarButtonPressed:(UIBarButtonItem *)sender {
if (self.tableView.editing == NO) {
UIBarButtonItem *myButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(editBarButtonPressed:)];
self.navigationItem.rightBarButtonItem = myButton;
[self.tableView setEditing:YES animated:YES];
[self.navigationItem setLeftBarButtonItem:self.addButton animated:YES];
} else {
UIBarButtonItem *myButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:#selector(editBarButtonPressed:)];
self.navigationItem.rightBarButtonItem = myButton;
[self.tableView setEditing:NO animated:YES];
[self.navigationItem setLeftBarButtonItem:self.backButton animated:NO];
}
}
Hope this answers your question. :)
br denrase
The back arrow button is your navigation controller button. So if you want to disappear the same button then you have to write this code below:-
self.navigationItem.hidesBackButton=YES;
Now if you want to add your custom button on the navigation controller then use below code:-
UIBarButtonItem *customButton =
[[UIBarButtonItem alloc]
initWithTitle:#"Add"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(yourMethod:)];
self.navigationItem.rightBarButtonItem = customButton;
You can programmatically hide the back button when your table view begins editing, and then add the "Add" button to the left of your navigation bar.
[self.navigationItem setHidesBackButton:YES];
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(addButtonPressed)];
[self.navigationItem setLeftBarButtonItem:addButton];
Then, when the user presses Done, replace the "Add" button with the Back button:
[self.navigationItem setHidesBackButton:NO];
self.navigationItem.leftBarButtonItem = self.navigationItem.backBarButtonItem;

backBarButtonItem in navigation controller not showing up

I started my xcode project from a NavigationViewController template. And now, when I push a view, that view comes up with an edit button by default and no back bar button. I have commented out the editButton code and the corresponding setEditing delegate method. But I can not get the back button to show up. What am I doing wrong?
Pushing the new view:
PlaylistViewController *playlistViewController = [[PlaylistViewController alloc] init];
playlistViewController.managedObjectContext = self.managedObjectContext;
[self.navigationController pushViewController:playlistViewController animated:YES];
[playlistViewController release];
in my PlaylistViewController:
- (void)viewDidLoad {
[super viewDidLoad];
self.title = #"";
// self.navigationItem.leftBarButtonItem = self.editButtonItem;
// doesn't matter if this is here or not
self.navigationItem.hidesBackButton = false;
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(insertNewObject)];
self.navigationItem.rightBarButtonItem = addButton;
[addButton release];
}
When you tell a UINavigationController to pushViewController, it automatically sets the navigationItem.leftBarButtonItem to be a button with the title of the previous UIViewController's title.
If no title is set, the default text is "Back".
If the title is set to "", no button will display at all.
self.title = #"";
Try changing this text and your back button should match the text set here.
Or you can manually override the text of the leftBarButtonItem from your new UIViewController.

Adding a UIBarButtonItem programmatically to UINavigationBar

I dropped in a UINavigationBar in UIInterfaceBuilder. I present this view modally and just want a UIBackBarButton to return to my last view. I have an outlet and property to this UINavigationBar declared. I thought in my viewDidLoad method, I could create a UIBackButton like this:
UIBarButtonItem *backButton =
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonItemStyleBordered
target:self
action:#selector(goBack)];
self.navigationItem.backBarButtonItem = backButton;
[backButton release];
But I do not see my UIBackBarButtonItem on the UINavigationBar. I think I am doing something wrong here since I don't think my UINavigationBar knows I'm trying to add this UIBackBarButtonItem to it in this way. Would I have to do create an NSArray, put the button in it, and setItems for the NavigationBar instead?
I'm confused on how the navigationItem property works vs the setItems of the UINavigationBar as well. Any help would be appreciated. Thanks!
You are trying to set the Back Button Item in a modal view which doesn't add a backBarButtonItem. This what causes the Button (or any sort of back button for that matter) not to show. The backBarButtonItem is mainly for use with Pushed View Controllers which have a Back Button added from the parent (next item below) when you push a new view controller (top item). The Apple UINavigationItem Documentation says:
When this item is the back item of the navigation bar—when it is the next item below the top item—it may be represented as a back button on the navigation bar. Use this property to specify the back button. The target and action of the back bar button item you set should be nil. The default value is a bar button item displaying the navigation item’s title.
To get the Back Button on the left side like you wish, Try changing
self.navigationItem.backBarButtonItem = backButton;
to
self.navigationItem.leftBarButtonItem = backButton;
making a call such as this from a view controller
{
NextViewController* vcRootView = [[NextViewController alloc] initWithNibName:#"NextView" bundle:[NSBundle mainBundle]];
UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:vcRootView];
[vcRootView release];
[self.navigationController presentModalViewController:navController animated:YES];
[navController release];
}
will present NextViewController as a Modal view on the calling view and NextViewController will have a navigationController for it.
In The NextViewController implementation file all you need is this
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem* backButton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:self
action:#selector(barButtonBackPressed:)];
self.navigationItem.leftBarButtonItem = backButton;
[backButton release];
}
-(void)barButtonBackPressed:(id)sender{
[self dismissModalViewControllerAnimated:YES];
}
to have the back button to dismiss the modalview. Hope it helps.
Use below code snippet :
//Add button to NavigationController
UIBarButtonItem *backButton =
[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(#“back”, #"")
style:UIBarButtonItemStylePlain
target:self
action:#selector(goBack)];
self.navigationItem.leftBarButtonItem = backButton;
//Perform action on back Button
- (void) goBack { // Go back task over-here
}
Different style types available are :
UIBarButtonItemStylePlain, UIBarButtonItemStyleBordered, UIBarButtonItemStyleDone
You may use this setters without creation new UIBarButtonItem:
[self.navigationItem.leftBarButtonItem setAction:#selector(doBackButton:)];
[self.navigationItem.leftBarButtonItem setTarget:self];

Resources