首页 文章

UITableView - 更改节 Headers 颜色

提问于
浏览
293

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

EDIT :应考虑适用于iOS 6及更高版本的answer provided by DJ-S . 接受的答案已过时 .

29 回答

  • 5

    基于@Dj S答案,使用Swift 3.这在iOS 10上运行良好 .

    func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
        // Background color
        view.tintColor = UIColor.black
    
        // Text Color
        let headerView = view as! UITableViewHeaderFooterView
        headerView.textLabel?.textColor = UIColor.white
    }
    
  • 2

    在iOS 7.x中,我有一个使用静态表视图单元的项目 . willDisplayHeaderView不会触发 . 但是,这种方法可行:

    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    {
        NSLog(@"%s", __FUNCTION__);
        CGRect headerFrame = CGRectMake(x, y, w, h);    
        UIView *headerView = [[UIView alloc] initWithFrame:headerFrame];  
        headerView.backgroundColor = [UIColor blackColor];
    
  • 8

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

    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];
    
  • 3

    希望 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;
    }
    

    Swift:

    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
    }
    

    2017年更新:

    Swift 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
        }
    

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

  • 663

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

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

    -(void)tableView:(UITableView *)tableView 
        willDisplayHeaderView:(UIView *)view 
        forSection:(NSInteger)section
    

    部分委托方法

    例如:

    - (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];
    }
    

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

    Swift 3

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

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

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

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

  • 1

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

    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()
    
    }
    
  • 2

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

    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
    {
        return 30;
    }
    
  • 3

    如果您不想创建自定义视图,还可以更改颜色(需要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;
        }
    }
    
  • 13

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

  • 4

    设置截面区域的背景和文本颜色:(感谢 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]];
        }
    }
    
  • 16

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

    • 选择表格视图

    • 转到属性检查器

    • 列出项目

    • 向下滚动到查看子 Headers

    • 更改"background"

    Have a look here

  • 49

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

    - (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;
    }
    
  • 10

    Swift 4

    要更改UITableView部分的 Headers 视图的 background colortext label colorfont ,只需为表视图覆盖 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)
    }
    

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

  • 18

    对于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
    

    }

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

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

    祝好运 .

  • 0

    SWIFT 2

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

    • 首先转到Storyboard并选择Table View

    • 转到属性检查器

    • 列出项目

    • 向下滚动到“查看”

    • 更改"Background"

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

    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
    
    }
    
  • 2

    我知道它的回答,以防万一,在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
        }
    
  • 2

    iOS 8+

    func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
            tableView.tableHeaderView?.backgroundColor = UIColor.blue()
    }
    
  • 0
    -(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 whiteColor]; // substitute your color here
                 content.backgroundColor = color;
                 [castView.textLabel setTextColor:[UIColor blackColor]];
            }
     }
    
  • 9

    我认为这段代码并不是那么糟糕 .

    func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let headerView = tableView.dequeueReusableHeaderFooterViewWithIdentifier(MyHeaderView.reuseIdentifier) as MyHeaderView
        let backgroundView = UIView()
        backgroundView.backgroundColor = UIColor.whiteColor()
        headerView.backgroundView = backgroundView
        headerView.textLabel.text = "hello"
        return headerView
    }
    
  • 1

    在iOS 7.0.4中,我使用自己的XIB创建了一个自定义标头 . 之前没有提到任何事情 . 它必须是UITableViewHeaderFooterView的子类才能使用 dequeueReusableHeaderFooterViewWithIdentifier: ,而且看起来这个类对于背景颜色非常顽固 . 所以最后我添加了一个名为customBackgroudView的UIView(你可以使用代码或IB),然后将它设置为's backgroundColor property. In layoutSubviews: I set that view'帧到边界 . It work with iOS 7 and gives no glitches.

    // in MyTableHeaderView.xib drop an UIView at top of the first child of the owner
    // first child becomes contentView
    
    // in MyTableHeaderView.h
    @property (nonatomic, weak) IBOutlet UIView * customBackgroundView;
    
    // in MyTableHeaderView.m
    -(void)layoutSubviews;
    {
        [super layoutSubviews];
    
        self.customBackgroundView.frame = self.bounds;
    }
    // if you don't have XIB / use IB, put in the initializer:
    -(id)initWithReuseIdentifier:(NSString *)reuseIdentifier
    {
        ...
        UIView * customBackgroundView = [[UIView alloc] init];
        [self.contentView addSubview:customBackgroundView];
        _customBackgroundView = customBackgroundView;
        ...
    }
    
    
    // in MyTableViewController.m
    -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    {
        MyTableHeaderView * header = [self.tableView
                                              dequeueReusableHeaderFooterViewWithIdentifier:@"MyTableHeaderView"];
        header.customBackgroundView.backgroundColor = [UIColor redColor];
        return header;
    }
    
  • 31

    只需更改 Headers 视图图层的颜色即可

    - (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
    {
      UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0,    tableView.bounds.size.width, 30)] autorelease];
     headerView.layer.backgroundColor = [UIColor clearColor].CGColor
    }
    
  • 4

    如果有人需要迅速,保持 Headers :

    override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view = UIView(frame: CGRect(x: 0,y: 0,width: self.tableView.frame.width, height: 30))
        view.backgroundColor = UIColor.redColor()
        let label = UILabel(frame: CGRect(x: 15,y: 5,width: 200,height: 25))
        label.text = self.tableView(tableView, titleForHeaderInSection: section)
        view.addSubview(label)
        return view
    }
    
  • 21

    在我的情况下,它的工作方式如下:

    let headerIdentifier = "HeaderIdentifier"
    let header = self.tableView.dequeueReusableHeaderFooterView(withIdentifier: headerIdentifier)
    header.contentView.backgroundColor = UIColor.white
    
  • 371

    使用RubyMotion / RedPotion,将其粘贴到TableScreen中:

    def tableView(_, willDisplayHeaderView: view, forSection: section)
        view.textLabel.textColor = rmq.color.your_text_color
        view.contentView.backgroundColor = rmq.color.your_background_color
      end
    

    奇迹般有效!

  • 3

    我通过控制台日志从Xcode收到消息

    [TableView]不推荐在UITableViewHeaderFooterView上设置背景颜色 . 请将具有所需背景颜色的自定义UIView设置为backgroundView属性 .

    然后我创建一个新的UIView并将其作为HeaderView的背景 . Xcode表示,这不是一个好的解决方案,但很容易 .

  • 13

    虽然 func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) 也可以正常工作,但您可以在不实现其他委托方法的情况下实现此目的 . 在你 func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? 方法中,你可以使用 view.contentView.backgroundColor = UIColor.white 而不是 view.backgroundView?.backgroundColor = UIColor.white 这是无效的 . (我知道 backgroundView 是可选的,但即使它在那里,如果没有实现 willDisplayHeaderView 也不会这样做

  • 6

    使用UIAppearance,您可以为应用程序中的所有 Headers 更改它,如下所示:

    UITableViewHeaderFooterView.appearance() . backgroundColor =theme.subViewBackgroundColor

  • 0

    只需设置背景视图的背景颜色:

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

相关问题