自iOS6以来,UINavigationBar中的标题未对齐

4
自从iOS 6以来,我在我的应用程序中使用自定义样式时遇到了几个问题。我使用自定义字体和几个UIAppearance代理。一个问题是我无法理解的标题在我的UINavigationBar中的错位。在iOS 5中,一切正常且正确对齐。
由于iOS 6已经发布并且自定义样式很常见,我认为这不是一个错误,而是我误解了iOS 6的一些新变化。
我已经搜索文档以查找可以在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];

1
很抱歉,这不是一个答案,但我想说我曾经看到过类似的问题,尽管只出现在iOS 5上。我只在使用自定义字体时才会遇到这些问题。因此,作为一个实验,您可以尝试看看如果使用标准字体是否可以解决问题。关于这个主题,您可以查看我的问题:http://stackoverflow.com/questions/12504556/uinavigationbar-title-behaves-oddly-when-specifying-a-font-using-uiappearance-w - Snips
它在标准字体下运行良好。我刚试过了。 - wiseindy
2个回答

12

我也遇到了同样的问题,但我注意到只要导航栏有返回按钮或右侧栏按钮(如表格编辑),就不会发生这种情况。当导航到新的视图控制器然后返回到第一个视图控制器时,标题的对齐也被固定了...

我认为发生的原因是iOS基于默认字体计算标题框架的位置,而我使用的字体稍微小一些,导致标题向左偏离中心。

我的当前解决方法是在 viewWillAppear 中调用 setNeedsLayout。看起来可以解决问题。

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

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

5
如果有帮助的话,你可以调整标题的(至少)垂直位置:
[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:4 forBarMetrics:UIBarMetricsDefault];

(这个问题帮助我找到了解决这个问题的方法:UINavigationBar自定义标题位置


标题已经奏效,现在我该怎么做才能让我的其他物品也这样?我只有一个背部物品。 - Charles Jr

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接