2012-10-09 16 views
4

自從iOS 6以來,我在我的應用程序中使用自定義樣式有幾個問題。我使用自定義字體和幾個UIAppearance代理。我無法理解的問題是UINavigationBar中的標題錯位。在iOS 5中一切正常,並正確對齊。自iOS6以來UINavigationBar中未對齊的標題

由於iOS6已經發布並且自定義樣式並不少見,我認爲這不是一個錯誤,而是我對iOS6的一些新變化的誤解。

misalignment

我搜索的文本對齊方式對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

+0

它適用於標準字體。剛試過了。 – wiseindy

回答

12

同樣的問題,但我注意到它時,導航欄有任何後退按鈕或不會發生右鍵欄按鈕(表格編輯)。標題上的對齊方式在導航到新的視圖控制器時又被固定,然後返回到第一個視圖控制器...

我認爲發生的事情是iOS計算標題框架的位置,基於默認的字體,因爲我使用的字體有點小,標題在中心左側偏離了一點。

我目前的修復方法是在viewWillAppear中調用setNeedsLayout。似乎工作。

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

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

這應該被標記爲正確的答案。很好的解決方法。 – wiseindy

5

如果有幫助,你可以調整(至少)標題的垂直位置:

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

(這個問題幫我找到了解決這個問題:UINavigationBar custom title position

+0

標題的作品,現在我怎麼能爲我的項目做到這一點?我只有一個回來的項目。 –