2013-06-26 67 views
1

我有一个问题在我的导航控制器中更改标题的字体。问题与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上看起来仍然相同。

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

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

+0

http://www.declan-software.com/blog/2010/12/14/programatically-set-the-title-of-a-uinavigationbar/ – Mital

+0

隐藏应用程序中的状态栏并再次检查。 –

+0

为什么你不使用标签? – jamil

回答

0

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

0

设置导航栏图像的ios5

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

设置导航栏图像iOS6的

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

后比用您的标题导航栏中心创建中心的标签。