Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Center navigation bar title vertically

I have a simple storyboard containing a navigation controller with a static table view controller. I have added a view controller and connected it to one of the cells via a push segue. One thing I have noticed is that the navigation bar title doesn't center properly vertically with the back button and UIBarButtonItem. Here is an image of what it looks now.enter image description here

Notice that it is all aligned to the bottom. What I want is, is a way to either move the title down or move the buttons up. If the buttons were moved up, the back button arrow should be moved as well, and allow to return to the main table view.

Here is the code I'm using to set the UIBarButtonItem font in the view controller (If it helps) :

[clearButton setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"ChalkboardSE-Regular" size:15], NSFontAttributeName, [UIColor whiteColor], NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];

And here is what I'm using to set the font for the back button in the app delegate, didFinishLaunchingWithOptions::

[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:[UIFont fontWithName:@"ChalkboardSE-Regular" size:15]}forState:UIControlStateNormal];

like image 861
Milo Avatar asked Nov 05 '13 08:11

Milo


Video Answer


2 Answers

You can move the title down using: [[UINavigationBar appearance] setTitleVerticalPositionAdjustment:(float) forBarMetrics:UIBarMetricsDefault];

like image 109
erkanyildiz Avatar answered Oct 14 '22 12:10

erkanyildiz


SWIFT version for iOS 8+

  self.navigationController!.navigationBar.setTitleVerticalPositionAdjustment(adj, forBarMetrics: .Default)
  • self is a UIViewController:

Note: Consider specifying adjustments for the other bar metrics options, to refine the offset for landscape mode and compact devices, because one offset may not look right on all phones in all positions.

like image 23
clearlight Avatar answered Oct 14 '22 12:10

clearlight