Title of navigation bar isn't centred after deleting back button title - ios

When I deleted the title text of my back button, the text of my navigation title is not centred.
I used
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#" " style:UIBarButtonItemStylePlain target:nil action:nil];
and
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60) forBarMetrics:UIBarMetricsDefault];
in both my viewDidLoad and viewWillAppear
this is a screenshot of the result
Any idea how I can fix this?

The UINavigationBar displays the backBarButtonItem of the previous view controller, so you'll need to make sure you are setting that and not the backBarButtonItem of the displayed view controller. The call to setBackButtonTitlePositionAdjustment(_:forBarMetrics:) is not necessary.

Related

Is it possible to add a UIBarButtonItem besides a UISegmentedControl?

I've added a UISegmentedControl to a UINavigation Bar, and when I tried to add a UIBarButtonItem programmatically in viewDidLoad viewController using the following code:
UIBarButtonItem *timeTableBarButton = [[UIBarButtonItem alloc]
initWithImage:[UIImage imageNamed:#"blueRoundRect"]
style:UIBarButtonItemStylePlain
target:self
action:#selector(popUpTimeTable)];
self.navigationItem.rightBarButtonItem = timeTableBarButton;
There are no button showing at all. I wonder since I already have a UISegmentedControl, is it possible to do so?
It is possible to add the Bar button in navigation bar and there is no way your segment control should be blocking right bar button item. Please make sure that your self.navigationItem is not nil.

How to change the backButtonItem title on a UIViewController?

I feel daft asking this question, but I haven't been able to get this to work. I have a NavController->TableViewController->WebViewController. I'm trying to change the title of the backButton on the WVC from the default "Back" to "Reference".
This should be a simple matter of setting the text property of the Back button either in Storyboard (which isn't working) or in code of my WebViewController's viewDidLoad method:
self.navigationItem.backBarButtonItem.title = #"Reference";
Is this just a bug or am I missing something?
Put this in your tableviewController
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"Reference" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = backButton;
Screen shot
From document
If you want to specify a custom image or title for the back button, you can assign a custom bar button item (with your custom title or image) to this property instead.
You should set it in your TableViewController, not in your WebViewContorller
Solved: I had added a grey activity indicator in a small view in the nav bar of the WVC. After I removed it, the code worked fine. Thanks #Leo, #Jacky, #Jaleel!
this work for me
self.navigationController.navigationBar.topItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Reference" style:UIBarButtonItemStylePlain target:nil action:nil];

Never show back button with "back" as title

I have the back buttons translated to german ("Zurück"). However, it seems that when there isn't enough space (because of the length of the title) it is shown as "back". And if there is only very little space, no title is shown at all, only the chevron "<".
Is there a way for it to not show the back buttons with the "back" title, but only as "<" if there isn't enough space for "Zurück"?
You can override loadView and put this code in your view controller:
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"" style:UIBarButtonItemStylePlain target:nil action:nil];
You could calculate the text length before that in order to decide if you want to show the back button with title or without it.
Edit
It's better to do this in a UIViewController superclass and have all your view controllers extend that class.
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60)
forBarMetrics:UIBarMetricsDefault];
Then you can remove the back button item title.
If you use Storyboard, you can set navigation attributes inspector Back Button with space.
You can follow this link

iOS 7 NavigationBar Back Button Custom Image with no Label

I am trying to create a custom back button on a navigation bar. I start from the following:
// Nav bar - back button
[[UINavigationBar appearance] setTintColor:COLOR_WHITE];
[[UINavigationBar appearance] setBackIndicatorImage:[[UIImage imageNamed:#"navMenuBackButton"]
imageWithAlignmentRectInsets:UIEdgeInsetsMake(6.0, -6.0, 6.0, -6.0)]];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[UIImage imageNamed:#"navMenuBackButton"]];
The image is 34x34 points so does not centre properly without the image alignment. The current problem is trying to get rid of the "Back" label without setting a blank title for each screen or making any changes on the ViewController itself.
Any idea's? Thank you
Disable leftBarButtonItem and rightBarButtonItem. Try the following.
[navigationItem.backItem.leftBarButtonItem setEnabled:NO];
[navigationItem.backItem.rightBarButtonItem setEnabled:NO];
[navigationItem.backItem setHidesBackButton:YES];
Did you try this :
NSString *backString = #"";
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:backString
style:UIBarButtonItemStyleDone
target:nil
action:nil];
[[self navigationItem] setBackBarButtonItem:backButton];

Can't get the right bar button item on a navigation bar?

I'm trying to have a button on the right side of my navigation controller - after this works I'd like the button to be as near to invisible as possible while still working. Maybe setting to high transparency or changing the colour to the navigation bar colour (whatever is cheaper for memory).
First though I can't seem to get a visible or working button on the right side of my navigation bar:
self.navigationController.navigationItem.rightBarButtonItem =
[[UIBarButtonItem alloc] initWithTitle:#"coin" style:UIBarButtonItemStyleDone
target:self action:#selector(changeToInAppScene:)];
You have to assign to self.navigationItem.rightBarButtonItem (the navigation item of the current view controller), not self.navigationController.navigationItem.rightBarButtonItem .
- (void)viewWillAppear:(BOOL)animated
{
UIBarButtonItem *rightBar = [[UIBarButtonItem alloc]initWithTitle:#"Coin" style:UIBarButtonItemStylePlain target:self action:#selector(changeToInAppScene:)];
self.navigationItem.rightBarButtonItem = rightBar;
}

Resources