I have a NavigationController based iOS7 app , on this I want to hide the back button text which is displayed along with the chevron. Is there a way out to this ? I tried setting empty string to the back button title , tried empty title on previous view as well seems like if it finds empty title it replaces that with "Back" text.
Please help
Thanks
Finally ended up solving it as follows , this one worked perfect.
self.navigationController.navigationBar.topItem.title = #"";
from this link Removing the title text of an iOS UIBarButtonItem
But if you navigate from previous view to next view you can see that the title of the previous view navigation bar vanishes when i put the above mentioned solution in viewDidDisappear of viewWillDisappear of previous view, which isn't an elegant solution in storyboard based UINavigationController scenario , in another situation i finally decided to use a bar button and set its image as per the native back button chevron, this gives better results.
The answer proposed by #vishal has a serious drawback: it removes the title from controller A if you navigate back from A to B.
Here is a safer solution to apply on controller A before pushing controller B:
self.navigationController.navigationBar.topItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"" style:UIBarButtonItemStylePlain target:nil action:nil];
And for swift:
if let topItem = controller.navigationController?.navigationBar.topItem {
topItem.backBarButtonItem = UIBarButtonItem(title: "", style: UIBarButtonItemStyle.Plain, target: nil, action: nil)
}
If you want to hide back button title into all your app, put this in you App Delegate:
#implementation UINavigationItem (myCustomization)
-(UIBarButtonItem *)backBarButtonItem
{
return [[UIBarButtonItem alloc] initWithTitle:#"" style:UIBarButtonItemStyleBordered target:nil action:nil];
}
#end
tested on iOS 7
For hide the back button of navigation controller ,try this one:
[self.navigationItem setHidesBackButton:YES animated:YES];
[self.navigationItem setBackBarButtonItem:nil];
[self.navigationItem setLeftBarButtonItem:nil animated:NO];
may it will help you.
happy coding...:)
The simplest solution is to remove the back button title with
navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .Plain, target: nil, action: nil)
in viewWillAppear on the presenting view controller. Note, presenting not presented.
From Removing the title text of an iOS UIBarButtonItem.
Related
In my application,I need to show the previous viewController title to current viewController back title.
Its working perfectly in iOS6.
In iOS7,automatically the "back" title displayed other than the previous viewController title.
how to fix the issue in iOS7?
In iOS 7 you will not be allowed to set the back button's title to be any longer than 11 characters.
To avoid changing the title of the view controller, but to change the back button's title, you need to do this:
In the previous view controller (the one that will have the next view controller pushed on top of it) you need to set the backBarButtonItem like so:
/**
* Notifies the view controller that its view was added to a view hierarchy.
*
* #param animated If YES, the view was added to the window using an animation.
*/
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
self.title = #"My Title Can Be Long";
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"ThisIsLimit"
style:UIBarButtonItemStylePlain
target:nil
action:nil];
}
Now, when the next view controller is pushed on top of it, the back button will be whatever title you put in the backBarButtonItem.
Due to low reputation I cannot add a comment so I'm posting an answer while this is not actually an answer.
But,
self.navigationController.navigationBar.topItem.title = #"";
which is written in one of the answers, is equivalent to:
self.title = #"";
try this,
self.navigationController.navigationBar.topItem.title = #"";
iOS 7 will automatically replace your back button title with "Back" or even remove the title altogether in order to fit the title of current navigation item. You probably shouldn't try to do anything about it except maybe try and make your titles shorter.
if you want to make short title you can do as below
self.title = #"SOME REALLY LONG NAVIGATION BAR TITLE";
UILabel* label=[[UILabel alloc] initWithFrame:CGRectMake(0,0, 200, 40)];
label.text=self.navigationItem.title;
label.adjustsFontSizeToFitWidth=YES;
self.navigationItem.titleView=label;
i have a view controller, which is standalone and has two left UIBarButtonItem, however when i push it, i want to have these two buttons + the back button
i tried
- (void)viewDidLoad
{
[super viewDidLoad];
// back
if (self.navigationController.navigationItem.backBarButtonItem) {
self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:self.navigationController.navigationItem.backBarButtonItem, self.barButtonFilter, self.barButtonFilterContacts, nil];
} else {
self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:self.barButtonFilter, self.barButtonFilterContacts, nil];
}
}
if there is a back button, than add, else replace
but i didnt work
I am not able to get your problem but according to your caption you want to replace you back button of UINavigationController with a bar button item, in that case you simply need to have a custom button in place of back Button:
UIBarButtonItem *backButton= [[UIBarButtonItem alloc] initWithTitle:#"yourTitle" style:UIBarButtonItemStylePlain target:self action:#selector(someFunction:)];
self.navigationItem.rightBarButtonItem = backButton;
[backButtonrelease];
If this is not your problem please elaborate.
So the issue here is that a UINavigationBar can only have one leftButtonItem and one rightButtonItem. But What you can do is in the center of the UINavigationBar you can have a UIView. You can use this to place the buttons on.
Someone has the code here: adding-buttons-to-the-titleview-of-navigationbar-without-having-to-repeat-code
From Apple's iOS Human Interface Guidelines:
Use a toolbar instead of a navigation bar if you need to offer a
larger set of controls, or you do not need to enable navigation.
Avoid crowding a navigation bar with additional controls, even if
there appears to be enough space. The navigation bar should contain no
more than a view’s current title, the back button, and one control
that manages the view’s contents. If, instead, you use a segmented
control in the navigation bar, the bar should not display a title and
it should not contain any controls other than the segmented control.
http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/mobilehig/UIElementGuidelines/UIElementGuidelines.html
In my app I have UINavigationController with two UIViewControler's (first and second). The first view controller contain UITableView. When a cell of table is touched the second view controller is pushed. What I want is to set custom text to back button at the second view. Here is the code in second view.
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.leftBarButtonItem =
[[[UIBarButtonItem alloc] initWithTitle:#"Go back"
style:UIBarButtonItemStyleBordered
target:nil
action:nil] autorelease];
}
The problem is that the text of back button is changed but second view controller is not removed from the stack when the button is touched.
This has been already asked so many times:
The back button (self.navigationItem.backBarButtonItem) must be set instead of left button and not in the second controller, but in the first one (root).
The 'back' button applies to the controller doing the pushing. In this case, you would want to assign the custom back button in your 'first' UIViewController. When you push the second view controller, the back button will show the proper text
You are trying to set the propert of the leftbarbuttonitem.
To avail the default properties of back bar button, just use
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
This would pop out the pushed view from the navigation controller and also enable you to go back to the last view.
I'm following the CoreDataRecipes app for modaly showing the add screen when I want to add a new item. However I cannot get the bar to display at the top so I can press 'Done' or 'Cancel'.
In the xib calling the modal controller I have the + button linked to modally sliding up the controller via IB.
I have the below in my modal controller
self.navigationItem.title = #"Add";
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStyleBordered target:self action:#selector(cancel)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Save" style:UIBarButtonItemStyleDone target:self action:#selector(save)];
self.navigationController.navigationBarHidden = NO;
In my viewDidLoad
The modal controller displays fine except there is no bar so I cannot leave that screen.
You need to add it before the popover is actually presented.
Where you create the modal popover, you need to create it inside a UINavigationController first.
So, do the following.
PopoverView *foo = [[PopoverView alloc] initWithNibName:#"PopoverView" bundle:nil];
// Here you pass through properties if you need too.
// ...
UINavigationController *navC = [[UINavigationController alloc] initWithRootView:foo];
[foo release];
[self.navigationController presentModalViewController:navC animated:YES];
That will give the modal view the navigation bar which you're trying to edit.
Alternatively, you could maintain your storyboard segue. In Xcode, select the view controller you are trying to transition to and embed it in a navigation controller.
Now in the viewDidLoad of that view controller, add:
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStyleBordered target:self action:#selector(cancel)];
and lastly the callback:
- (void)cancel {
[self dismissModalViewControllerAnimated:YES];
}
Or if you just need the look of the bar, not exactly its functionality, you could drag the Navigation Bar (UINavigationBar) or Toolbar (UIToolbar) controll from the Media Library panel onto your view and go from there.
I had a similar predicament whereby I was loading a UITableViewController in a containerView. The containerView was inside a UIViewController which was being presented in a modal fashion.
I, like you, needed the navigation bar to have a title and a Done/Cancel button.
After overflowing the stack, I finally did this -
Dragged a UIView as the first item in the Table View in the IB. This automatically took a height of 44 pts and snapped to the top. It also shifted my first section downwards.
I dragged a UIButton (Done button) inside this view. Created an IBOutlet to it and called
[self dismissViewControllerAnimated:YES completion:nil];
Disclaimer:
1) This fake nav-bar will scroll along with the tableview.
2) This might not be a solution for what Bot has asked, but it's an option for others who might be looking for something similar.
I am using the following function via a notification to load a right button on my UINavigationBar, and even though I can trace out the button and verify it is allocated, it does not show up...any ideas?
EDIT 4/6/2011, 2:42PM
So, something interesting...the width always reports as 0.0...
- (void)showRightBarButton:(id)sender
{
NSLog(#"Showing button");
UIBarButtonItem *button = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:#selector(showPI:)];
[button setTitle:#"This Button"];
self.navigationItem.rightBarButtonItem = button;
//[[self.navigationItem rightBarButtonItem] setWidth:50];
NSLog(#"Button width is %f.", self.navigationItem.rightBarButtonItem.width);
[button release];
}
[self.view setNeedsDisplay];
You're right. That line isn't needed. As far as the rest of the code goes I don't see what's wrong with it. The only thing I've come up with so far is that self isn't the currently displayed view controller or that you're missing a navigation controller. Perhaps you've created your UINavigationBar yourself instead of using a navigation controller?
Anyway, for easier debugging I would suggest the following:
- (void)showRightBarButton:(id)sender
{
NSLog(#"Showing button");
UIBarButtonItem *button = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:#selector(showPI:)];
self.navigationItem.rightBarButtonItem = button;
[button release];
}
EDIT: The width isn't interesting. It's always 0.0 unless you specify it yourself.
The problem is that you're adding the button in the wrong place. You're not supposed to add the button to the navigation item of the navigation controller, but to the navigation item of the controller that is currently displayed by the navigation controller.
You say you're using an NSNotification to trigger the addition of the bar button item. Where are you registering for the notification? Is it possible that the notification is being received before your view is loaded?
If you're registering for notifications in -init you may want to set a flag on your view controller that you should be displaying the bar button item and then reference that flag in -viewDidLoad to optionally display it.
Or you could just register for the notification in -viewDidLoad.