How can I change the back navigation bar button item text? - iphone

I am working on a navigation based application. Until now I am able to do this:
self.navigationItem.title=self.Category;
UIBarButtonItem *btnNewGame = [[UIBarButtonItem alloc] initWithTitle:#"New Game" style:UIBarButtonItemStyleBordered
target:self action:#selector(btnNewGameclicked:)];
self.navigationItem.rightBarButtonItem = btnNewGame;
[btnNewGame release];
and then I can call the method provided by the selector. No problems since here.
What happens now is that I have the left bar button item, the title and the right bar button item.
Problem
The title on the left bar button item is not back, but it appears to be the same as the navigation item's title. How can I change this? Any suggestions?
Thanks.

default behaviour of the left bar button item, is to display the title of the previous view Controller
if you want to change the text on the left bar button item, you need to replace the left (back) button like this
UIBarButtonItem *myBarButtonItem = [[UIBarButtonItem alloc] init];
myBarButtonItem.title = #"Back"; // or whatever text you want
self.navigationItem.backBarButtonItem = myBarButtonItem;
[myBarButtonItem release];

As per your question what i understand , your problem is that the title of leftbarbuttonitem is not Back, this is because you gave the previous screen some title.That's why leftbarbutton item shows the title of navigation-item and not as Back.To remove that you must have erase the title of previous view or do it pro-grammatically as given by Matt.

And why not
self.navigationItem.leftBarButtonItem.title = #"MyTitle";
?

Try this way in the viewDidLoad method:
self.navigationController.navigationBar.topItem.title = #"YourBack";
This worked for me.

Related

How can I use the pointy back button for my own buttons/navbar items?

As I'm sure you know, navbars inside a navigation controller stack get a back button that shaped sort of like an arrow with a pointy end on the left. I want to use this button image for my own uibuttons and navbar items, but it's missing from from the attribute inspector > bar item > image drop down menu. Where is this graphic located and how can I use it?
here's a screen of the button to which I refer:
If you want to do this programatically -
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithImage:yourImage style:UIBarButtonItemStyleBordered target:nil action:nil];
self.navigationItem.backBarButtonItem = button;
[button release];
You can use like below in xib-

UISegmentedControl's alignment get change when hiding UINavigationItem buttons

I have an UISegmentedControl in the title view of the navigation bar. Also i have right bar button(plus) and left bar button(Edit) as shown in the below image..
I'm hiding the two bar buttons (here, edit & plus buttons) when the ID segment is clicked. This will be like..
I'm using the following code to hide bar button items.
- (void)segmentClicked {
segment = segmentedControl.selectedSegmentIndex;
self.navigationItem.leftBarButtonItem = leftBarButton; //leftBarButton is an instance of UIBarButtonItem
self.navigationItem.leftBarButtonItem = rightBarButton; //rightBarButton is an instance of UIBarButtonItem
if (segment == 0) {
//Do something for segment 1 click
}
else if (segment == 1) {
//Do something for segment 2 click
}
else {
self.navigationItem.leftBarButtonItem = nil;
self.navigationItem.rightBarButtonItem = nil;
}
}
My problem is, when i switch from segment1(Department) or segment2(Name) to segment3(ID), the UISegmentedControl alignment get changed. How can i make it stable?
Thanks in Advance
bar button items align automatically..
if you remove other items the segmented control will shift.
What you need to do is use a 'fixed space' bar item to position your segmented control...
instead of making your leftBarButtonItem nil, change it to a fixed space item of size required to keep the segmented control in place..
if you want to see how it works before implementing, just put a tool bar in a xib and add 'fixed space' and 'flexible space' items..
EDIT:
its pretty straight forward. Just look at the uibarbuttonitem class. create a system bar button item object of type fixed space and set it's width as per your need. and set it as your left bar button:
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
item.width = 50;
self.navigationItem.leftBarButtonItem = item;
[item release];
EDIT 2:
another suggestion - use shorter text for the segments. like use Dept. instead of Department. if your segmented control in title view is short enough it wont be affected by bar buttons.

How to Hide the UInavigationBar Rightbarbutton item?

I added the info button to the navigation bar using below code:
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
[infoButton addTarget:self action:#selector(showImage:)
forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:infoButton];
Now i want to hide this button at some part of the code based on some conditions. But i didn't find any hide property for right bar button item in the navigation bar?
For making hidden, try with assigning nil to your rightBarButtonItem like below.
self.navigationItem.rightBarButtonItem = nil ;
Best option is to use buttonItem.enabled = NO to indicate the functionality isn't available at the moment. That should, in most cases, be the right behavior.
However if you intend to make it disappear, the best way would be to store a reference to the bar button. Set the rightBarButtonItem to nil when you want it to disappear and set it to the stored reference when you want it to be displayed.
If you have multiple UIBarButtonItems and you just want to remove one, you can do this:
NSMutableArray *barButtonItems = [self.toobbar.items mutableCopy];
[barButtonItems removeObject:self.buttonToRemove];
[self.toolbar setItems:[barButtonItems copy] animated:NO];
If you just want to 'hide' it visually:
Swift 3:
self.navigationItem.rightBarButtonItem?.tintColor = UIColor.clear
self.navigationItem.rightBarButtonItem?.isEnabled = false

Problems adding a UIBarButtonItem programmatically or editing one inserted via IB

I'm working on project that uses a tab bar at the bottom to flip through 5 sections of my app. One of these sections loads a map view which then spawns a web view. This was done with Round Rect Buttons at first, but they got in the way of the views, so I decided to switch to a navigation bar. In Interface Builder I inserted a navigation item and within it two Bar Button Items (left and right side). I've gotten the functionality for both button to work by control dragging from file's owner to fire the method associated with each button. This works fine and launches my web view, but what I would like to be able to do and have yet to figure out how to so far, is changing one of the buttons to a "done" or "back" button once in the web view. I've tried creating both buttons programmatically but neither would appear. This is the code I tried using to accomplish this...
UIBarButtonItem *updatePosition = [[UIBarButtonItem alloc] initWithTitle:#"Update Position"
style:UIBarButtonItemStylePlain target:self action:#selector(findMe)];
self.navigationItem.leftBarButtonItem = updatePosition;
[updatePosition release];
When I run the app, the button does not show up on the navigation item I inserted using Xcode. The only way I can get the buttons to appear is by inserting them with Interface Builder as I previously mentioned. But then I can not change the left button to a "back" or "done" button once I've brought up the new view. I tried running this code to change the style of the button on the UIBarButtonItem I inserted using IB with this code
self.navigationItem.leftBarButtonItem.style = UIBarButtonItemStyleDone;
But to no avail.
Here are some Utility methods to help you along the way:
+ (UIBarButtonItem *)setRightBarButtonItem:(SEL)action target:(id)sender withImage:(NSString *)imageName {
UIImage *buttonImage = [UIImage imageNamed:imageName];
UIBarButtonItem *buttonItem = [[[UIBarButtonItem alloc] initWithImage:buttonImage
style:UIBarButtonItemStyleBordered
target:sender
action:action] autorelease];
return buttonItem;
}
+ (UIBarButtonItem *)setRightBarButtonItem:(SEL)action target:(id)sender withBarButtonSystemItem:(UIBarButtonSystemItem)systemItem {
UIBarButtonItem *buttonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:systemItem
target:sender
action:action] autorelease];
return buttonItem;
}
Usage:
// Done button.
self.navigationItem.rightBarButtonItem = [Utility setRightBarButtonItem:#selector(doneTapped:) target:self withBarButtonSystemItem:UIBarButtonSystemItemDone];

adding icon between back button & title of navigation bar

I want to put an icon by side of the title of my navigation bar. I'd prefer not to implement it as a custom titleView, because then i'll need to create a custom titleView for each controller i put on the stack (and I have pretty deep nesting). I'm adding currently as an UIImageView to a navigationBar. My problem is to calculate exactly this icon's horizontal position. It depends on the width of the back button, which has each time another title. How do I calculate this back button frame? Googling on it seems doesn't bring any reasonable results.
Thanks in advance
You could calculate the size of a label, with
your text in it and experiment with what the button will add on..
CGSize s = [label sizeWithFont:_font];
I ran into this same issue. My solution was to add the icon as one of the leftBarButtonItems, after a flexible space. This pushes it up against the title.
UIBarButtonItem *space = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease];
UIView *icon = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"titleIcon"]] autorelease];
UIBarButtonItem *iconItem = [[[UIBarButtonItem alloc] initWithCustomView:icon] autorelease];
NSArray *items = [NSArray arrayWithObjects: space, iconItem, nil];
self.navigationItem.leftBarButtonItems = items;
self.navigationItem.leftItemsSupplementBackButton = YES;
It's not quite the same as creating a custom titleView, however, in two ways: (1) the title text remains centered if possible, with the icon to its left, rather than centering the title and icon together; and (2) when you push a new view onto the stack, the icon does not slide to the left with the title; instead it fades out, like the other bar button items.
So, depending on your requirements, this solution may or may not work for you. But at least it avoids trying to match the standard title text attributes in a custom title view. (As far as I can discover, there is no automatic way to do that.)