更改UITableView分区标题的颜色

19

好的,我知道之前已经有人问过这个问题,所以请原谅我再次提问。似乎应该有一种更简单的方法来完成这个任务。

有没有一种“简单”的方法来更改UITableView部分标题的背景颜色?我知道可以使用代理方法“viewForHeaderInSection”为UITableView提供一个自定义UIView来用作部分标题,但我真正想做的只是设置“tintColor”。搜索栏上有一个tintColor属性,为什么部分标题没有呢。

如果我使用viewForHeaderInSection创建自己的自定义UIView,我还必须创建自己的标签。我必须将我的标签样式和位置设置成与Apple标准相同。我必须将“背景”样式和大小设置成与苹果标准相同。

我找不到一种简单的方法来获取部分标题然后更改其颜色。我是否遗漏了什么或者我真的必须以艰难的方式来完成。

如果我必须以艰难的方式来完成,是否有人拥有符合Apple标准的所有样式信息? - 栏是“半透明”的 - 栏顶部和底部有些阴影 - 标签具有特定的大小、位置、阴影等

谢谢。再次抱歉反复问这个问题。

6个回答

30

使用较新的UIAppearance方式,在iOS 6.0以上版本中,您只需执行以下操作,例如:

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

1
我需要把这个放在哪里? - Ulaga
在iOS 10中似乎没有任何效果。 - elsurudo

21

使用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 class] 应该在 iOS 6.0 之前返回 nil,因此该方法在 6.0 之前不应起作用。 - Maxim Kholyavkin

12
这是我自己花了很长时间去处理的问题,最终发现没有办法只改变片段标题的 tintColor。我想出的解决方案是截取片段标题背景的屏幕截图,在 Photoshop 中更改其色调,然后将其用作片段标题的背景。然后只需要布置标签即可。
就像你所说的那样,要使用的是 viewForHeaderInSection 委托方法。
这是我发现有效且看起来像 Apple 默认效果的方法:
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 情况下为双倍大小)的图片,会在头部区域上横向平铺。

希望能对您有所帮助!


1
因为iPhone 4的分辨率是原来的两倍?所以在这个例子中,“headerbackground.png”将是1x22,我还会有另一张名为“headerbackground@2x.png”的图片,它将用于iPhone 4(现在也适用于新的iPod touch)。更多信息请阅读此处:http://developer.apple.com/library/ios/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/SupportingResolutionIndependence/SupportingResolutionIndependence.html - neilkimmett
自己截屏显示文本的正确左缩进为12像素,而不是10。如下所示:CGFloat leftInset = 12.0f; UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(leftInset, 0, tableSize.width - leftInset, headerImg.size.height)]; - MoDJ

8

如果你像其他答案中那样改变外观,那么它将应用于整个应用程序。 如果你只想为特定的表格视图进行更改,请执行以下操作:

-(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

iOS 9.0已经弃用了UIAppearance中的appearanceWhenContainedIn方法,但你可以使用appearanceWhenContainedInInstancesOfClasses方法和一个数组来实现完全相同的功能。

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

0

我们可以像这样更改标题的背景颜色:

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int){         
      let tableHeader = view as! UITableViewHeaderFooterView        
      tableHeader.backgroundView?.backgroundColor = UIColor.white     
    }

请不要仅仅发布代码作为答案,还要提供解释您的代码是如何解决问题的。带有解释的答案通常质量更高,更有可能吸引赞同。 - Boken

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接