3 回答

TA貢獻(xiàn)1820條經(jīng)驗(yàn) 獲得超3個(gè)贊
現(xiàn)代的方法
現(xiàn)代方式,對(duì)于整個(gè)導(dǎo)航控制器......在加載導(dǎo)航控制器的根視圖時(shí)執(zhí)行此操作一次。
[self.navigationController.navigationBar setTitleTextAttributes: @{NSForegroundColorAttributeName:[UIColor yellowColor]}];
但是,這似乎對(duì)后續(xù)視圖沒(méi)有影響。
經(jīng)典的方法
舊的方式,每個(gè)視圖控制器(這些常量適用于iOS 6,但如果想在iOS 7外觀上按照視圖控制器進(jìn)行操作,您將需要相同的方法但具有不同的常量):
您需要使用一個(gè)UILabel
作為titleView
的navigationItem
。
標(biāo)簽應(yīng)該:
有明確的背景顏色(
label.backgroundColor = [UIColor clearColor]
)。使用粗體20pt系統(tǒng)字體(
label.font = [UIFont boldSystemFontOfSize: 20.0f]
)。有50%alpha(
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5]
)的黑色陰影。您還需要將文本對(duì)齊設(shè)置為居中(
label.textAlignment = NSTextAlignmentCenter
(UITextAlignmentCenter
對(duì)于較舊的SDK)。
將標(biāo)簽文本顏色設(shè)置為您想要的任何自定義顏色。您確實(shí)需要一種不會(huì)導(dǎo)致文本混合成陰影的顏色,這種顏色難以閱讀。
我通過(guò)反復(fù)試驗(yàn)解決了這個(gè)問(wèn)題,但我想出的價(jià)值最終太簡(jiǎn)單了,以至于他們不能成為蘋果選擇的。:)
如果你想驗(yàn)證這一點(diǎn),放棄這一代碼到initWithNibName:bundle:
中PageThreeViewController.m
的蘋果的NavBar樣品。這將用黃色標(biāo)簽替換文本。除了顏色之外,這應(yīng)該與Apple代碼生成的原始文件無(wú)法區(qū)分。
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{ self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // this will appear as the title in the navigation bar UILabel *label = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease]; label.backgroundColor = [UIColor clearColor]; label.font = [UIFont boldSystemFontOfSize:20.0]; label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5]; label.textAlignment = NSTextAlignmentCenter; // ^-Use UITextAlignmentCenter for older SDKs. label.textColor = [UIColor yellowColor]; // change this color self.navigationItem.titleView = label; label.text = NSLocalizedString(@"PageThreeTitle", @""); [label sizeToFit]; } return self;}
編輯:另外,閱讀Erik B的答案如下。我的代碼顯示了效果,但是他的代碼提供了一種更簡(jiǎn)單的方法來(lái)將其放在現(xiàn)有視圖控制器上。

TA貢獻(xiàn)1906條經(jīng)驗(yàn) 獲得超10個(gè)贊
我知道這是一個(gè)非常古老的線程,但我認(rèn)為知道新用戶iOS 5帶來(lái)了一個(gè)用于建立標(biāo)題屬性的新屬性會(huì)很有用。
您可以使用UINavigationBar setTitleTextAttributes
來(lái)設(shè)置字體,顏色,偏移和陰影顏色。
此外,您可以為UINavigationBars
整個(gè)應(yīng)用程序中的所有設(shè)置相同的默認(rèn)UINavigationBar標(biāo)題文本屬性。
例如:
NSDictionary *navbarTitleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys: [UIColor whiteColor],UITextAttributeTextColor, [UIColor blackColor], UITextAttributeTextShadowColor, [NSValue valueWithUIOffset:UIOffsetMake(-1, 0)], UITextAttributeTextShadowOffset, nil];[[UINavigationBar appearance] setTitleTextAttributes:navbarTitleTextAttributes];

TA貢獻(xiàn)1821條經(jīng)驗(yàn) 獲得超5個(gè)贊
在iOS 5中,您可以通過(guò)以下方式更改navigationBar標(biāo)題顏色:
navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor yellowColor]};
- 3 回答
- 0 關(guān)注
- 1033 瀏覽
添加回答
舉報(bào)