2013-06-26 69 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]; 

後比用您的標題導航欄中心創建中心的標籤。