【问题标题】:Misaligned title in UINavigationBar since iOS6自 iOS6 以来 UINavigationBar 中的标题未对齐
【发布时间】:2012-09-29 15:40:24
【问题描述】:

从 iOS 6 开始,我在应用程序中使用自定义样式时遇到了几个问题。我使用自定义字体和几个UIAppearance 代理。我无法理解的一个问题是我的 UINavigationBar 中的标题未对齐。在 iOS 5 中,一切正常并且正确对齐。

由于 iOS6 已经发布并且自定义样式并不少见,我认为这不是错误,而是我对 iOS6 的一些新变化的误解。

我在文档中搜索了一个文本对齐方法来调用 UIAppearance 代理,但我找不到这样的方法。

我使用以下代码行在整个应用程序中设置 UINavigationBar 的样式:

[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"NavigationBarBackground"] 
                                   forBarMetrics:UIBarMetricsDefault];

[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"NavigationBarBackground"] 
                                   forBarMetrics:UIBarMetricsLandscapePhone];

[[UINavigationBar appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                      [UIColor whiteColor], UITextAttributeTextColor, 
                                                      [UIFont fontWithName:@"Corbel-Bold" size:14.0], UITextAttributeFont,
                                                      nil]];


[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTintColor:[UIColor whiteColor]];
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                      [UIColor ceBlueColor], UITextAttributeTextColor,
                                                      [UIColor whiteColor], UITextAttributeTextShadowColor, 
                                                      [NSValue valueWithUIOffset:UIOffsetMake(0, 1)], UITextAttributeTextShadowOffset, 
                                                      [UIFont fontWithName:@"Corbel" size:0.0], UITextAttributeFont, 
                                                      nil]
                                            forState:UIControlStateNormal];

[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -3) forBarMetrics:UIBarMetricsDefault];

【问题讨论】:

  • 恐怕不是答案,但我想说我没有看到不同的问题,尽管只有 iOS 5。我只在使用自定义字体时看到问题。因此,作为一个实验,您可能想尝试看看如果您使用标准字体,问题是否会消失。请参阅我关于此主题的问题:stackoverflow.com/questions/12504556/…
  • 它适用于标准字体。刚试了一下。

标签: ios fonts uinavigationbar uiappearance


【解决方案1】:

如果有帮助,您可以(至少)调整标题的垂直位置:

[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:4 forBarMetrics:UIBarMetricsDefault];

(这个问题帮我找到了解决这个问题的方法:UINavigationBar custom title position

【讨论】:

  • 标题有效,现在我该如何为我的项目执行此操作?我只有一件后备品。
【解决方案2】:

这里有同样的问题,但我注意到当导航栏有后退按钮或右栏按钮(表格编辑)时不会发生这种情况。导航到新视图控制器然后返回第一个视图控制器时,标题上的对齐方式也已修复...

我认为发生的情况是iOS根据默认字体计算标题框架的位置,因为我使用的字体有点小,标题有点偏向中心左侧。

我当前的修复是在 viewWillAppear 中调用 setNeedsLayout。似乎工作正常。

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    if (self.navigationItem.hidesBackButton || self.navigationItem.rightBarButtonItem == nil) {
        [self.navigationController.navigationBar setNeedsLayout];
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-13
    • 2018-10-10
    • 2012-09-22
    相关资源
    最近更新 更多