【问题标题】:Changing the color of UITableView section headers更改 UITableView 部分标题的颜色
【发布时间】:2011-03-18 17:11:15
【问题描述】:

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

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

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

我找不到一种方法来简单地询问节标题然后更改它的颜色。我是否遗漏了什么,或者我真的必须以艰难的方式做到这一点。

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

谢谢。再一次,很抱歉再次问这个问题。

【问题讨论】:

    标签: iphone uikit uitableview


    【解决方案1】:

    这是我花了很长时间与自己搏斗的东西,却发现没有办法只更改节标题的 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 像素 x 22 像素(iPhone 4 的两倍)图像,它将在整个标题长度上重复出现。

    希望这会有所帮助!

    【讨论】:

    • 因为 iPhone 4 的分辨率是原来的两倍?所以在那个例子中,“headerbackground.png”将是 1x22,我还有另一个名为“headerbackground@2x.png”的图像,它将用于 iPhone 4(现在是新的 iPod touch)。欲了解更多信息,请阅读此处:developer.apple.com/library/ios/#documentation/iPhone/…
    • 做我自己的屏幕截图表明文本的正确左插图是 12 像素,而不是 10。像这样:CGFloat leftInset = 12.0f; UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(leftInset, 0, tableSize.width - leftInset, headerImg.size.height)];
    【解决方案2】:

    使用更新的 UIAppearance 做事方式,在 iOS 6.0 以上的版本中你可以这样做,例如:

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

    【讨论】:

    • 我需要把这个放在哪里?
    • 在iOS 10好像没有效果
    【解决方案3】:

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

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

    应该可以在 iOS 6.0+ 上运行。可以在任何地方调用(viewDidLoad 等)。

    【讨论】:

    • [UITableViewHeaderFooterView class] 应该在 iOS 6.0 之前返回 nil,所以这个方法在 6.0 之前应该不能工作
    【解决方案4】:

    如果您更改外观,就像在其他答案中一样,它是应用程序范围内的。 如果您只想针对特定的 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];
    }
    

    【讨论】:

      【解决方案5】:

      从 iOS 9.0 开始不推荐使用 UIAppearance 方法 appearanceWhenContainedIn;相反,您仍然可以使用 appearanceWhenContainedInInstancesOfClasses 方法和数组执行完全相同的操作。

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

      【讨论】:

        【解决方案6】:

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

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

        【讨论】:

        • 请不要只发布代码作为答案,还要解释您的代码的作用以及它如何解决问题的问题。带有解释的答案通常质量更高,更有可能吸引投票。
        猜你喜欢
        • 2010-10-23
        • 2013-01-03
        • 1970-01-01
        • 2013-02-18
        • 2023-03-04
        • 2015-07-26
        • 2011-04-12
        • 2011-01-24
        • 2022-08-17
        相关资源
        最近更新 更多