【问题标题】:Center navigation bar title vertically垂直居中导航栏标题
【发布时间】:2013-11-16 01:06:21
【问题描述】:

我有一个简单的故事板,其中包含一个导航控制器和一个静态表视图控制器。我添加了一个视图控制器,并通过 push segue 将其连接到其中一个单元格。我注意到的一件事是导航栏标题没有正确垂直居中,后退按钮和UIBarButtonItem。 这是它现在的样子。

请注意,它都与底部对齐。我想要的是一种向下移动标题或向上移动按钮的方法。如果按钮向上移动,则后退按钮箭头也应移动,并允许返回主表视图。

这是我用来在视图控制器中设置UIBarButtonItem 字体的代码(如果有帮助的话):

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

这是我用来在应用程序委托中设置后退按钮的字体didFinishLaunchingWithOptions:

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

【问题讨论】:

    标签: ios objective-c uinavigationbar


    【解决方案1】:

    您可以使用以下命令将标题下移:[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:(float) forBarMetrics:UIBarMetricsDefault];

    【讨论】:

    • 谢谢!完美运行!
    • 是什么导致它开始不居中?
    • 一些字体渲染和属性字符串部分不同。
    【解决方案2】:

    适用于 iOS 8+ 的 SWIFT 版本

      self.navigationController!.navigationBar.setTitleVerticalPositionAdjustment(adj, forBarMetrics: .Default)
    
    • self 是一个 UIViewController:

    注意考虑为其他条形指标选项指定调整,以优化横向模式和紧凑型设备的偏移量,因为一个偏移量可能不会在所有位置的所有手机上看起来都正确。

    【讨论】:

      【解决方案3】:

      将标题下移:

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

      向上移动标题:

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

      【讨论】:

        【解决方案4】:

        在 Swift 5 中:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-09-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-01-23
          • 2017-07-17
          相关资源
          最近更新 更多