首页 文章

更改UITableView节 Headers 的颜色

提问于
浏览
19

好的,我知道之前已经问过,所以请原谅我再问一次 . 看起来似乎必须有一种更简单的方法来做到这一点 .

是否有一种“简单”的方式来更改UITableView部分 Headers 背景颜色?我知道我可以使用委托方法'viewForHeaderInSection'来传递UITableView自定义UIView用于节 Headers ,但我真正想做的就是设置'tintColor' . 搜索栏上有一个tintColor属性,为什么不用于节 Headers .

如果我使用viewForHeaderInSection来创建我自己的自定义UIView,我也必须创建自己的标签 . 我必须将标签的样式和位置看起来与Apple标准的其他部分一样 . 我必须将'背景'的样式和大小看起来像Apple标准的其余部分 .

我找不到简单地询问节 Headers 然后改变它的颜色的方法 . 我错过了什么,或者我真的必须这么做 .

如果我必须这么做,那么有人拥有符合Apple标准的所有样式信息吗? - 酒吧是'半透明' - 酒吧顶部和底部有一些阴影 - 标签有一定的大小,位置,阴影等

谢谢 . 再次,抱歉再次提出这个问题 .

5 回答

  • 29

    这是我花了很长时间与自己搏斗的事情,却发现没有办法只改变节头的tintColor . 我想出的解决方案是截取节 Headers 的背景,在Photoshop中更改它的色调,然后将其用作节 Headers 的背景 . 然后它只是一个布局标签的案例 .

    正如您所说,使用的是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的两倍)图像,它将在 Headers 的长度上重复出现 .

    希望这可以帮助!

  • 2

    使用更新的UIAppearance服务方式,在版本> iOS 6.0中,您可以这样做,例如:

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

    使用UIAppearance,您可以在 UITableView Headers 上更改 UILabel 文本颜色,如下所示:

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

    应该适用于iOS 6.0 . 可以在任何地方调用( viewDidLoad 等) .

  • 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];
    }
    
  • 19

    UIAppearance方法appearanceWhenContainedIn自iOS 9.0起已被弃用;相反,你仍然可以使用appearanceWhenContainedInInstancesOfClasses方法和数组完全相同的事情 .

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

相关问题