UINavigationBar标题对齐问题

时间:2013-06-26 09:30:57

标签: iphone ios uinavigationcontroller uinavigationbar

我在导航控制器中更改标题的字体时出现问题。

这是我的代码(来自我的UINavigationController的子类):

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSDictionary * attributes = @{UITextAttributeTextColor  :[UIColor whiteColor],
                                  UITextAttributeFont       :[UIFont fontWithName:@"Blanch" size:50],
                                  };
    self.navigationBar.titleTextAttributes  = attributes;
    CGFloat verticalOffset = -8;
    [[UINavigationBar appearance] setTitleVerticalPositionAdjustment:verticalOffset forBarMetrics:UIBarMetricsDefault];

    UIImage *image = [UIImage imageNamed:NAVBAR_IMAGE];
    [self.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
}

这就是它的样子:

enter image description here

如果我注释掉设置字体的行,这就是它的样子(正如预期的垂直偏移量):

enter image description here

垂直偏移的唯一原因是我认为它可能解决了这个问题。我不需要它,它现在被注释掉了,但它不会改变输出 - 它在NavBar上看起来仍然相同。

任何想法如何解决这个问题?我认为可能是自定义字体有很多行间距 我想这可能是问题。

我一直试图找到一种方法来改变标题的原点/高度/基线对齐方式,但却找不到办法来做任何这些。 知道如何解决这个问题吗?

2 个答案:

答案 0 :(得分:0)

创建一个容器UIView,其宽度与标签相同,高度为UINavigationBar(约46像素?)。将此容器视图上的clipsToBounds设置为YES。现在将标签添加到此容器视图中,并使容器视图为titleView。

答案 1 :(得分:0)

为ios5设置导航栏图像

if ([self.navigationController.navigationBar respondsToSelector:@selector( setBackgroundImage:forBarMetrics:)]){
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"YOUR_NAVIGATION_BAR_IMAGE"] forBarMetrics:UIBarMetricsDefault];
}

为ios 6设置导航栏图像

if([[UINavigationBar appearance] respondsToSelector:@selector( setBackgroundImage:forBarMetrics:)]){
    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"YOUR_NAVIGATION_BAR_IMAGE"] forBarMetrics:UIBarMetricsDefault];

然后在导航栏中心的中心创建带有标题的标签。