3 回答

TA貢獻1820條經驗 獲得超2個贊
現代的方法
現代方式,對于整個導航控制器......在加載導航控制器的根視圖時執行此操作一次。
[self.navigationController.navigationBar setTitleTextAttributes: @{NSForegroundColorAttributeName:[UIColor yellowColor]}];
但是,這似乎對后續視圖沒有影響。
經典的方法
舊的方式,每個視圖控制器(這些常量適用于iOS 6,但如果想在iOS 7外觀上按照視圖控制器進行操作,您將需要相同的方法但具有不同的常量):
您需要使用一個UILabel
作為titleView
的navigationItem
。
標簽應該:
有明確的背景顏色(
label.backgroundColor = [UIColor clearColor]
)。使用粗體20pt系統字體(
label.font = [UIFont boldSystemFontOfSize: 20.0f]
)。有50%alpha(
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5]
)的黑色陰影。您還需要將文本對齊設置為居中(
label.textAlignment = NSTextAlignmentCenter
(UITextAlignmentCenter
對于較舊的SDK)。
將標簽文本顏色設置為您想要的任何自定義顏色。您確實需要一種不會導致文本混合成陰影的顏色,這種顏色難以閱讀。
我通過反復試驗解決了這個問題,但我想出的價值最終太簡單了,以至于他們不能成為蘋果選擇的。:)
如果你想驗證這一點,放棄這一代碼到initWithNibName:bundle:
中PageThreeViewController.m
的蘋果的NavBar樣品。這將用黃色標簽替換文本。除了顏色之外,這應該與Apple代碼生成的原始文件無法區分。
- (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的答案如下。我的代碼顯示了效果,但是他的代碼提供了一種更簡單的方法來將其放在現有視圖控制器上。

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

TA貢獻1821條經驗 獲得超5個贊
在iOS 5中,您可以通過以下方式更改navigationBar標題顏色:
navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor yellowColor]};
- 3 回答
- 0 關注
- 1021 瀏覽
添加回答
舉報