Milo
Milo

Reputation: 5091

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];

Upvotes: 9

Views: 6974

Answers (4)

Kashif
Kashif

Reputation: 4632

In swift 5:

self.navigationBar.setTitleVerticalPositionAdjustment(ajustment, for:.default)

Upvotes: 0

Karthik damodara
Karthik damodara

Reputation: 1955

To move title down :

self.navigationController!.navigationBar.setTitleVerticalPositionAdjustmen(+Value, forBarMetrics: .Default)

To move title up:

self.navigationController!.navigationBar.setTitleVerticalPositionAdjustmen(-Value, forBarMetrics: .Default)

Upvotes: 3

clearlight
clearlight

Reputation: 12625

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.

Upvotes: 6

erkanyildiz
erkanyildiz

Reputation: 13234

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

Upvotes: 9

Related Questions