2012-04-11 105 views
22

我想定製我的UINavigationBar的字體,使用iOS 5的下面的代碼在我的應用程序委託的application:didFinishLaunchingWithOptions自定義字體UINavigationBar的

if ([[UINavigationBar class] respondsToSelector:@selector(appearance)]) 
{ 
    [[UINavigationBar appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: 
                  [UIColor whiteColor], UITextAttributeTextColor, 
                  [UIColor blackColor], UITextAttributeTextShadowColor, 
                  [NSValue valueWithUIOffset:UIOffsetMake(1, 0)], UITextAttributeTextShadowOffset, 
                  [UIFont fontWithName:kDefaultFont size:0.0], UITextAttributeFont, 
                  nil]]; 
} 

它工作正常,並使用我的字體呈現的導航欄。大。

references我發現建議你可以使用零的字體大小,它會調整字體以滿足您的導航欄(使用了橫向佈局的短導航欄略小的字體)。而且它的確選擇了一種很適合導航欄高度的字體大小。但是,如果您從縱向到橫向並返回,導航欄的標題標籤的寬度會變得很大,所以顯示爲例如「Long Titlebar」的標題時,首次查看它時看起來不錯在縱向方向上,當您在橫向(使用適當更小的字體)中查看時看起來很好,但是當我回到縱向時,字體正確地恢復爲較大的字體,但標題文本本身被截斷,變爲「Long ..」。 。「即使有很多空間可以放滿整個標題。有沒有其他人看到這種行爲時使用0.0的字體大小?很明顯,我可以指定一個實際的字體大小(在這種情況下,我沒有看到這種截斷行爲),但是我手動計算出要使用的大小。更糟糕的是,橫向和縱向的字體大小是相同的,因此我現在使用的字體大小適合較短的橫向導航欄標題,而標題小於需要放置在較高縱向導航欄中的字體大小。

有沒有人有過使用setTitleTextAttributes來改變字體大小在縱向和橫向之間改變字體的方式,但是在你返回到肖像後不能截斷標題時使用setTitleTextAttributes來改變字體景觀?我即將開展各種各樣的解決方案,但如果您有任何關於此問題的經驗,請告訴我。

更新:

在提交這個bug蘋果的過程中,我決定將演示如何重現該問題:

  1. 在Xcode 4.3中創建新的iOS主從應用。 2。

  2. 將上面的setTitleTextAttributes代碼放在應用程序代理的application:didFinishLaunchingWithOptions(我使用font @「GillSans」)。

  3. 轉到MasterViewController並添加一行說self.title = @"Long Title";

  4. 註釋掉UIBarButtonItem *addButton代碼。

  5. 運行該程序。注意正確的標題說「長標題」。旋轉到風景。仍然看起來不錯。旋轉回肖像,標題現在顯示「Long ...」,即使有足夠的空間。

  6. 奇怪的是,如果您恢復UIBarButtonItem *addButton代碼,則標題按原樣工作。但是,如果您要麼消除UIBarButton項目,要麼將其替換爲使用initWithTitle而不是initWithBarButtonSystemItem的按鈕,則在從縱向旋轉到橫向然後再回到縱向後,導航欄標題會出現問題。

+0

我有一個問題,那就是我的自定義字體導航欄標題在第一次顯示時被「過早截斷」,但之後很好。似乎是一個iOS 6主義。無論如何,發現我可以設置字體大小爲0.0解決了問題。謝謝! – mharper 2012-10-05 01:06:43

+1

有趣的是,在使用此方法的iOS5.1模擬器中,標題在* pushModalViewController轉換期間被截斷*,但轉換完成後顯示正常。在iOS 6.0模擬器上,它按預期工作。 – 2012-10-17 09:32:50

回答

1

以下是我對這個問題的解決方法,而這裏的旁邊顯示我的解決辦法的實施在每種情況下,以提醒自己,爲什麼我執行這段代碼的註釋:

// when using an appearance proxy to set a custom font for the navigation bar (generally in 
// application:didFinishLaunchingWithOptions: in the appDelegate code) for both iOS 5 & 6, 
// there's a glitch that incorrectly auto-truncates the title in the following cirumstances: 
// 
// 1) when a 0.0 value is used for UITextAttributeFont in the titleTextAttributes dictionary 
// and a device/simulator running pre-iOS 5 rotates back to portrait from landscape 
// solution: perform [self.navigationController.navigationBar setNeedsLayout] in 
//    didRotateFromInterfaceOrientation: the view controller in which the 
//    auto-truncation is incorrectly occurring for systemVersion < 6.0 
// 
// 2) when a view initially loads running iOS 6 for a non-0.0 value for the UITextAttributeFont 
// in the titleTextAttributes dictionary 
// solution: perform [self.navigationController.navigationBar setNeedsLayout] 
//    in viewDidLoad in the view controller in which the auto truncation is 
//    incorrectly occurring for systemVersion >= 6.0 

所以,對於我確定與使用用於UITextAttributeFont 0.0值,但必須繼續支持的iOS5,我使用溶液中的情況下,(1):

#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_6_0 
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { 
    if (UIInterfaceOrientationIsPortrait(self.interfaceOrientation) 
     && UIDevice.currentDevice.systemVersion.floatValue < 6.0) 
     [self.navigationController.navigationBar setNeedsLayout]; 
} 
#endif 

和在叔他幾代遺留代碼,我想支持iOS 6,並修復視圖第一次出現時的故障,而不必重寫MyAppAppearance類的方法,我已經爲我的[UINavigationBar appearance] titleTextAttributes設置了非0.0值,我發現它更容易實施該解決方案(2)這樣的:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_6_0 
    if (UIDevice.currentDevice.systemVersion.floatValue >= 6.0) 
#endif 
     [self.navigationController.navigationBar setNeedsLayout]; 

    // … other viewDidLoadCode 

(和__IPHONE_OS_VERSION_MIN_REQUIRED支架只是幫助提醒我,如果需要的話,今後它的代碼最終會消失,並可能留下來。)

看更確切地說,這裏發生了什麼,特別是在旋轉的情況下,運行緩慢動畫模擬器切換。

1

我認爲一個好的解決方案是在設備旋轉後刷新導航欄的標題。像

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { 

     self.navigationItem.title = @"Your title"; 

} 

希望這有助於!

+0

那當然是我嘗試的第一件事情之一。沒有快樂。 – Rob 2012-04-14 13:34:30

3

順便說一下,我忽略了指出,蘋果回覆我的錯誤報告,承認這是一個已知的問題。希望它很快就會解決!

1

基於來自@Abramodj(不起作用)的迴應,我試了這個。據推測,該解決方案沒有發生任何事情,因爲該系統注意到該文本實際上沒有改變。切換爲無,然後再重新排序。

經測試確實可以在iOS5.0上使用。

// iOS5 has a bug where if you switch orientation the title bar text gets cut off... 
-(void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation  { 
    self.navigationItem.title = @""; 
    self.navigationItem.title = @"Your Title"; 
} 
0

即使不像Ben Clayton所描述的那樣進行方向切換,也可能會出現在通過外觀應用更改後切斷UINavigationBar標題文本的問題。我見過這個問題發生在只支持縱向的應用上。

[self.navigationItem setTitle:@""]; 
[self.navigationItem setTitle:@"The real title"]; 

即使在這種情況下也能正常工作。

順便說一句,我從iOS 5開始就遇到了這個問題。現在只是在iOS 6中測試它,並且還在那裏。蘋果怎麼了?