2013-06-28 103 views
9

我在我的應用程序中使用了UINavigationBarUITabBar。 在第一個選項卡上,導航欄標題以默認背景色作爲白色標題正確顯示,但在第二個選項卡上,它不以相同方式工作。我沒有使用任何代碼來更改標題顏色或導航欄的tintColorNavigationBar標題以黑色顯示

第一視點:
http://img407.imageshack.us/img407/7192/4go.png

第二個視圖: http://img824.imageshack.us/img824/4493/idtk.png

爲什麼第二種觀點的UINavigationBar冠軍在這個黑顏色繪製?

+0

我想你應該設置顏色以第二視圖導航欄絲毫不差檢查出 – Jitendra

回答

26

通常,您不能更改默認顏色UINavigationBar標題。如果您想更改UINavigationBar標題的顏色,則需要自定義UINavigationBar。所以把你的第二個ViewController的代碼用於更多的理解。

編輯:

搜索後,我發現,您可以通過

self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:UITextAttributeTextColor]; 

該代碼是工作在iOS5的,後來改變UINavigationBar標題顏色。

+0

我試圖改變標題的顏色,但它不工作。還有一件事秒視圖控制器代碼是不會影響,如果我使用新的viewController沒有任何代碼,相同的問題正在發生..... – Rivan

+0

@rivan如果你在iOS 5上工作,然後把這個代碼在ViewDidLoad方法我試了它在我的項目它爲我工作:) – iPatel

+0

@ rivan-它是否適合你? – iPatel

1

此代碼更改所有導航欄的文本,使用此代碼文本可以自定義爲100%。

中的appDelegate

//custom text navBar 
    [[UINavigationBar appearance] setTitleTextAttributes: 
    [NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:0x73/255.0 green:0x47/255.0 blue:0x41/255.0 alpha:1.0], UITextAttributeTextColor, 
    [UIColor colorWithRed:0x1D/255.0 green:0x1D/255.0 blue:0x1B/255.0 alpha:1], UITextAttributeTextShadowColor, 
    [NSValue valueWithUIOffset:UIOffsetMake(0, 1)],UITextAttributeTextShadowOffset, 
    [UIFont fontWithName:@"yourFont" size:20], UITextAttributeFont, nil]]; 
+0

當我使用您的代碼時,它將更改firstview navigationController標題以黑色,並且所有位於firstNavigation(推視圖之後)的視圖均爲白色。 – Rivan

7

這將允許您更改大部分的上述建議,現在已被棄用顏色

NSDictionary *navbarTitleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys: 
             [UIColor whiteColor],UITextAttributeTextColor, 
             [UIColor blackColor],  UITextAttributeTextShadowColor, 
             [NSValue valueWithUIOffset:UIOffsetMake(-1, 0)], UITextAttributeTextShadowOffset, nil]; 

[[UINavigationBar appearance] setTitleTextAttributes:navbarTitleTextAttributes]; 
+0

不適用於我的情況。 – Rivan

11

,針對iOS 7的使用 -

NSDictionary *textAttributes = [NSDictionary dictionaryWithObjectsAndKeys: 
           [UIColor whiteColor],NSForegroundColorAttributeName, 
           [UIColor whiteColor],NSBackgroundColorAttributeName,nil]; 

self.navigationController.navigationBar.titleTextAttributes = textAttributes; 
self.title = @"Title of the Page"; 

此外,檢查可以設置的各種文本屬性的NSAttributedString.h。

+0

這對我很好。我正準備發佈此更新的iO7修復程序,並進行了投票! – Coughlin

4

使用這一位iOS7代碼,因爲UITextAttributeTextColor已被棄用。

self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor orangeColor] forKey:NSForegroundColorAttributeName]; 
8

試試這個在AppDelegate中:

[[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}]; 
+0

謝謝。這是正確的答案。你想在外觀上設置它,就像在一個普通的應用程序中一樣,你真的希望所有的導航欄看起來都一樣。 – n13