简体   繁体   English

垂直居中导航栏标题

[英]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.我添加了一个视图控制器,并通过 push segue 将其连接到其中一个单元格。 One thing I have noticed is that the navigation bar title doesn't center properly vertically with the back button and UIBarButtonItem .我注意到的一件事是导航栏标题没有与后退按钮和UIBarButtonItem正确垂直UIBarButtonItem Here is an image of what it looks now.这是它现在的样子。在此处输入图片说明

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) :这是我用来在视图控制器中设置UIBarButtonItem字体的代码(如果有帮助):

[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: :这是我用来设置应用程序委托中后退按钮的字体didFinishLaunchingWithOptions:

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

您可以使用以下方法将标题向下移动: [[UINavigationBar appearance] setTitleVerticalPositionAdjustment:(float) forBarMetrics:UIBarMetricsDefault];

SWIFT version for iOS 8+适用于 iOS 8+ 的 SWIFT 版本

  self.navigationController!.navigationBar.setTitleVerticalPositionAdjustment(adj, forBarMetrics: .Default)
  • self is a UIViewController: self是一个 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.注意考虑为其他条形指标选项指定调整,以优化横向模式和紧凑型设备的偏移量,因为一个偏移量可能无法在所有位置的所有手机上看起来都正确。

To move title down :下移标题:

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

To move title up:上移标题:

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

在快速 5:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM