UITableView - 更改节标题颜色

Ily*_*ski 326 cocoa-touch uitableview ios

如何在UITableView中更改节标题的颜色?

编辑:DJ-S提供答案应考虑适用于iOS 6及更高版本.接受的答案已过时.

Dj *_*j S 724

这是一个老问题,但我认为答案需要更新.

此方法不涉及定义和创建自己的自定义视图.在iOS 6及更高版本中,您可以通过定义来轻松更改背景颜色和文本颜色

-(void)tableView:(UITableView *)tableView 
    willDisplayHeaderView:(UIView *)view 
    forSection:(NSInteger)section
Run Code Online (Sandbox Code Playgroud)

部分委托方法

例如:

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
    // Background color
    view.tintColor = [UIColor blackColor];

    // Text Color
    UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
    [header.textLabel setTextColor:[UIColor whiteColor]];

    // Another way to set the background color
    // Note: does not preserve gradient effect of original header
    // header.contentView.backgroundColor = [UIColor blackColor];
}
Run Code Online (Sandbox Code Playgroud)

取自我的帖子:https: //happyteamlabs.com/blog/ios-how-to-customize-table-view-header-and-footer-colors/

斯威夫特3/4

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int){
    view.tintColor = UIColor.red
    let header = view as! UITableViewHeaderFooterView
    header.textLabel?.textColor = UIColor.white
}
Run Code Online (Sandbox Code Playgroud)

  • 这对我来说似乎没有用.文本颜色有效,但不是标题背景的色调.我在iOS 7.0.4上 (10认同)
  • user1639164,你可以使用header.backgroundView.backgroundColor = [UIColor blackColor]; 设置标题背景的色调. (10认同)
  • 我什至不知道这甚至已添加到SDK中。辉煌!绝对正确的答案。 (2认同)
  • @Kent显然已经有一段时间了,但是对于未来的人们来说,`header.contentView.backgroundColor = [UIColor blackColor];`选项将为您提供一个不透明的标题 (2认同)

Ale*_*lds 388

希望UITableViewDelegate协议中的这个方法可以帮助您入门:

Objective-C的:

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{
  UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)] autorelease];
  if (section == integerRepresentingYourSectionOfInterest)
     [headerView setBackgroundColor:[UIColor redColor]];
  else 
     [headerView setBackgroundColor:[UIColor clearColor]];
  return headerView;
}
Run Code Online (Sandbox Code Playgroud)

迅速:

func tableView(_ tableView: UITableView!, viewForHeaderInSection section: Int) -> UIView!
{
  let headerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.bounds.size.width, height: 30))
  if (section == integerRepresentingYourSectionOfInterest) {
    headerView.backgroundColor = UIColor.redColor()
  } else {
    headerView.backgroundColor = UIColor.clearColor()
  }
  return headerView
}
Run Code Online (Sandbox Code Playgroud)

2017年更新:

斯威夫特3:

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
    {
        let headerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.bounds.size.width, height: 30))
        if (section == integerRepresentingYourSectionOfInterest) {
            headerView.backgroundColor = UIColor.red
        } else {
            headerView.backgroundColor = UIColor.clear
        }
        return headerView
    }
Run Code Online (Sandbox Code Playgroud)

替换[UIColor redColor]UIColor您想要的任何一个.您可能还希望调整尺寸headerView.

  • 它还可以使用self.tableView.sectionHeaderHeight帮助调整节标题大小.否则,您可能无法看到为节标题显示的文本. (17认同)
  • 请注意,此答案(正确)将只返回没有内容的UIView. (11认同)
  • 这是非常过时的信息,只是创建另一个视图不是最好的答案.我们的想法是获得正确的视图并更改其上的颜色或色调.以下使用willDisplayHeaderView的答案是一种更好的方法. (7认同)

Doc*_*orG 97

以下是更改文本颜色的方法.

UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(10, 3, tableView.bounds.size.width - 10, 18)] autorelease];
label.text = @"Section Header Text Here";
label.textColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.75];
label.backgroundColor = [UIColor clearColor];
[headerView addSubview:label];
Run Code Online (Sandbox Code Playgroud)

  • 谢谢DoctorG - 这很有用.顺便说一句 - 为了保持dataSource提供的现有标签,我修改了第二行,如下所示:label.text = [tableView.dataSource tableView:tableView titleForHeaderInSection:section]; 可能是糟糕的形式,但它对我有用.也许这可以帮助别人. (18认同)
  • 我删除了自动释放并将其更改为显式版本.UITableView格式化方法被多次调用.尽可能避免使用自动释放. (3认同)

Les*_*rna 50

如果您想要具有自定义颜色的标题,可以执行此操作:

[[UITableViewHeaderFooterView appearance] setTintColor:[UIColor redColor]];
Run Code Online (Sandbox Code Playgroud)

这个解决方案从iOS 6.0开始运行良好.


Max*_*Max 31

以下解决方案适用于iOS 8+的Swift 1.2

override func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {

    // This changes the header background
    view.tintColor = UIColor.blueColor()

    // Gets the header view as a UITableViewHeaderFooterView and changes the text colour
    var headerView: UITableViewHeaderFooterView = view as! UITableViewHeaderFooterView
    headerView.textLabel.textColor = UIColor.redColor()

}
Run Code Online (Sandbox Code Playgroud)


why*_*yoz 21

不要忘记从代理中添加这段代码,或者在某些情况下,相对于视图/标签的高度,您的视图将被截断或显示在表格后面.

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}
Run Code Online (Sandbox Code Playgroud)


Ale*_*lex 21

不推荐在UITableViewHeaderFooterView上设置背景颜色.请contentView.backgroundColor改用.


Wil*_*sch 18

如果您不想创建自定义视图,还可以更改颜色(需要iOS 6):

-(void) tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
    if ([view isKindOfClass: [UITableViewHeaderFooterView class]]) {
        UITableViewHeaderFooterView* castView = (UITableViewHeaderFooterView*) view;
        UIView* content = castView.contentView;
        UIColor* color = [UIColor colorWithWhite:0.85 alpha:1.]; // substitute your color here
        content.backgroundColor = color;
    }
}
Run Code Online (Sandbox Code Playgroud)


Ste*_*eve 17

你可以在大约2秒内在main.storyboard上完成.

  1. 选择表格视图
  2. 转到属性检查器
  3. 项目清单
  4. 向下滚动到View子标题
  5. 换背景"

看看这里


Roo*_*ahi 13

设置截面区域的背景和文本颜色:(感谢William JockuschDj S)

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
    if ([view isKindOfClass: [UITableViewHeaderFooterView class]]) {
        UITableViewHeaderFooterView* castView = (UITableViewHeaderFooterView*) view;
        castView.contentView.backgroundColor = [UIColor grayColor];
        [castView.textLabel setTextColor:[UIColor grayColor]];
    }
}
Run Code Online (Sandbox Code Playgroud)


Nii*_*tse 12

斯威夫特4

要更改UITableView部分的标题视图的背景颜色,文本标签颜色字体,只需覆盖willDisplayHeaderView表视图,如下所示:

override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
        let header = view as! UITableViewHeaderFooterView
        header.backgroundView?.backgroundColor = .white
        header.textLabel?.textColor = .black
        header.textLabel?.font = UIFont(name: "Helvetica-Bold", size: 14)
} 
Run Code Online (Sandbox Code Playgroud)

这对我很有用; 希望它也对你有所帮助!


Mau*_*lik 10

以下是在标题视图中添加图像的方法:

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{
    UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)] autorelease];
    UIImageView *headerImage = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"top-gery-bar.png"]] autorelease];

    headerImage.frame = CGRectMake(0, 0, tableView.bounds.size.width, 30);

    [headerView addSubview:headerImage];

    return headerView;
}
Run Code Online (Sandbox Code Playgroud)


the*_*Dev 9

快速 5 +

willDisplayHeaderView方法中

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {

     //For Header Background Color
     view.tintColor = .black

    // For Header Text Color
    let header = view as! UITableViewHeaderFooterView
    header.textLabel?.textColor = .white
}
Run Code Online (Sandbox Code Playgroud)

我希望这可以帮助你 :]


Cor*_*ona 8

对于iOS8(Beta)和Swift,选择你想要的RGB颜色,试试这个:

override func tableView(tableView: UITableView!, viewForHeaderInSection section: Int) -> UIView! {
    var header :UITableViewHeaderFooterView = UITableViewHeaderFooterView()

    header.contentView.backgroundColor = UIColor(red: 254.0/255.0, green: 190.0/255.0, blue: 127.0/255.0, alpha: 1)
    return header
Run Code Online (Sandbox Code Playgroud)

}

("覆盖"是因为我在我的项目中使用UITableViewController而不是普通的UIViewController,但它不是强制更改节标题颜色)

您的标题文字仍会显示.请注意,您需要调整节标题高度.

祝好运.


小智 6

SWIFT 2

我能够通过添加模糊效果(这非常酷)成功更改了部分背景颜色.要轻松更改部分的背景颜色:

  1. 首先转到Storyboard并选择Table View
  2. 转到属性检查器
  3. 项目清单
  4. 向下滚动到View
  5. 换背景"

然后对于模糊效果,添加到代码:

override func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {

    // This is the blur effect

    let blurEffect = UIBlurEffect(style: .Light)
    let blurEffectView = UIVisualEffectView(effect: blurEffect)

    // Gets the header view as a UITableViewHeaderFooterView and changes the text colour and adds above blur effect
    let headerView: UITableViewHeaderFooterView = view as! UITableViewHeaderFooterView
    headerView.textLabel!.textColor = UIColor.darkGrayColor()
    headerView.textLabel!.font = UIFont(name: "HelveticaNeue-Light", size: 13)
    headerView.tintColor = .groupTableViewBackgroundColor()
    headerView.backgroundView = blurEffectView

}
Run Code Online (Sandbox Code Playgroud)


Dav*_*ord 6

Swift 4 让它变得非常简单。只需将其添加到您的班级并根据需要设置颜色。

override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
        view.backgroundColor = UIColor(red: 0.094, green: 0.239, blue: 0.424, alpha: 1.0)
    }
Run Code Online (Sandbox Code Playgroud)

或者如果一个简单的颜色

override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
        view.backgroundColor = UIColor.white
    }
Run Code Online (Sandbox Code Playgroud)

为 Swift 5 更新

override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
        view.tintColor = UIColor(red: 0.094, green: 0.239, blue: 0.424, alpha: 1.0)
    }
Run Code Online (Sandbox Code Playgroud)

或者如果一个简单的颜色

override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
        view.tintColor = UIColor.white
    }
Run Code Online (Sandbox Code Playgroud)

  • 在 iOS 13 中将“view.backgroundColor”替换为“view.tintColor”。 (6认同)

Sur*_*rma 6

对我来说,在浪费了 2 个小时后,以上都不起作用,这就是解决方案。就我而言,它是自定义视图,但由于某种原因我无法从故事板和视图的 awakeFromNib 更改它。

override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
        let header = view as! UITableViewHeaderFooterView
        header.contentView.backgroundColor = .white
    }
Run Code Online (Sandbox Code Playgroud)


ara*_*_86 5

我知道它的答案,以防万一,在Swift中使用以下内容

    override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let tableViewWidth = self.tableView.bounds

        let headerView = UIView(frame: CGRectMake(0, 0, tableViewWidth.size.width, self.tableView.sectionHeaderHeight))
        headerView.backgroundColor = UIColor.greenColor()

        return headerView
    }
Run Code Online (Sandbox Code Playgroud)