2010-07-20 93 views
19

好的,我知道這是以前問過的,所以請原諒我再問一次。這看起來似乎有一個更簡單的方法來做到這一點。更改UITableView節標題的顏色

是否有'簡單'的方式來更改UITableView節標題背景顏色?我知道我可以使用委託方法'viewForHeaderInSection'將UITableView作爲一個自定義的UIView用於節頭部,但我真正想要做的就是設置'tintColor'。在搜索欄上有一個tintColor屬性,爲什麼不是節標題。

如果我使用viewForHeaderInSection創建自己的自定義UIView,我也必須創建自己的標籤。我必須將標籤的樣式和位置看起來像蘋果標準的其餘部分。我必須設計風格並將背景大小設置爲Apple標準的其餘部分。

我找不到一個方法來簡單地問節標題,然後改變它的顏色。我是否錯過了一些東西,或者我是否真的必須這麼做。

如果我必須這麼做,難道有人擁有符合Apple標準的所有樣式信息嗎? - 巴是「半透明」 - 條具有在頂部的一些着色和底部 - 標籤具有一定的尺寸,位置,陰影等

感謝。再一次,很抱歉再次提出這個問題。

回答

12

這是我花了很長一段時間與自己爭鬥,只是發現,沒有辦法改變節頭的tintColor。我想出的解決方案是截取節頭的背景,在Photoshop中改變它的色調,然後用它作爲節頭的背景。那麼它只是一個佈置標籤的案例。

正如你所說,使用的是viewForHeaderInSection委託方法。

這裏是我發現的作品,看起來像蘋果默認:

UIView *customView = [[[UIView alloc] initWithFrame:CGRectMake(10.0, 0.0, 320.0, 22.0)] autorelease]; 
customView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"headerbackground.png"]];; 

UILabel *headerLabel = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease]; 
headerLabel.backgroundColor = [UIColor clearColor]; 
headerLabel.opaque = NO; 
headerLabel.textColor = [UIColor whiteColor]; 
headerLabel.font = [UIFont boldSystemFontOfSize:18]; 
headerLabel.shadowOffset = CGSizeMake(0.0f, 1.0f); 
headerLabel.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5]; 
headerLabel.frame = CGRectMake(11,-11, 320.0, 44.0); 
headerLabel.textAlignment = UITextAlignmentLeft; 
headerLabel.text = @"Header Label"; 
[customView addSubview:headerLabel]; 
return customView; 

這裏的「headerbackground.png」是1個像素由22像素(兩倍於iPhone 4)圖像,將讓重複整個頭部的長度。

希望這會有所幫助!

+0

爲什麼把iPhone 4翻一番?你可以請給這個信息的參考.. – Anand 2010-09-11 15:21:48

+1

因爲iPhone 4有兩倍的分辨率?所以在這個例子中,「headerbackground.png」將是1x22,我還會得到另一個名爲「[email protected]」的圖像,它將用於iPhone 4(現在是新的iPod touch)。欲瞭解更多信息請閱讀此處:http://developer.apple.com/library/ios/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/SupportingResolutionIndependence/SupportingResolutionIndependence.html – neilkimmett 2010-10-11 12:48:58

+0

做我自己的屏幕截圖表明正確的文本左側插入是12像素,而不是10.像這樣:CGFloat leftInset = 12.0f; UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(leftInset,0,tableSize.width - leftInset,headerImg.size.height)]; – MoDJ 2011-08-30 21:40:05

29

處事的新UIAppearance方式,在版本>的iOS 6.0,你可以這樣做,例如:

[[UITableViewHeaderFooterView appearance] setTintColor:[UIColor redColor]]; 
+1

我需要把這個? – Ulaga 2013-12-13 15:01:50

+0

似乎沒有任何效果在iOS 10 – elsurudo 2016-10-13 20:18:21

19

隨着UIAppearance你可以在你UITableView標題更改UILabel文本顏色是這樣的:

[[UILabel appearanceWhenContainedIn:[UITableViewHeaderFooterView class], nil] setTextColor:[UIColor whiteColor]]; 
[[UILabel appearanceWhenContainedIn:[UITableViewHeaderFooterView class], nil] setShadowColor:[UIColor whiteColor]]; 

應該在iOS 6.0+上運行。可以在任何地方調用(viewDidLoad等)。

+1

[UITableViewHeaderFooterView類]應返回零直到iOS 6.0,所以這種方法不應該工作,直到6.0 – Speakus 2014-01-03 15:08:17

+0

謝謝,我糾正了我的答案。 – user882209 2014-01-03 20:26:38

7

如果您改變外觀,就像在其他答案中一樣,它在應用程序範圍內。 如果你想改變只爲特定的tableview,執行:

-(void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section { 
    UITableViewHeaderFooterView * headerview = (UITableViewHeaderFooterView *)view; 
    headerview.contentView.backgroundColor = [UIColor greenColor]; 
    headerview.textLabel.textColor = [UIColor whiteColor]; 
} 
2

的UIAppearance方法appearanceWhenContainedIn已被棄用的iOS 9。0;相反,您仍然可以使用appearanceWhenContainedInInstancesOfClasses方法和一個數組完成同樣的事情。

[UILabel appearanceWhenContainedInInstancesOfClasses:@[[UITableViewHeaderFooterView class]]].textColor = [UIColor darkTextColor];