UITableView的 - 改款头彩

问题描述 投票:310回答:30

我怎样才能改变的UITableView节头的颜色?

编辑:answer provided by DJ-S应考虑iOS 6以上。接受的答案是过时的。

ios uitableview cocoa-touch
30个回答
385
投票

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

迅速:

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年更新:

斯威夫特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的尺寸。


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

11
投票

斯威夫特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)
} 

这非常适合我;希望它帮助你呢!


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

8
投票

对于iOS8上(测试版)和斯威夫特选择你想要的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 i'm,但it's不是强制性的改变节头彩)

你的标题的文本将仍然可以看到。请注意,您将需要调整节头的高度。

祝好运。


6
投票

SWIFT 2

我是能够成功地改变部分的背景颜色与添加的虚化效果(这是真的很酷)。轻松更改部分的背景色:

  1. 首先去故事板,并选择表视图
  2. 转到属性检查器
  3. 项目清单
  4. 向下滚动查看
  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

}

5
投票

我知道它的回答,为了以防万一,在斯威夫特使用以下

    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
    }

4
投票

的iOS 8+

func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
        tableView.tableHeaderView?.backgroundColor = UIColor.blue()
}

4
投票

基于@Dj的回答,用斯威夫特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
}

3
投票

我使用静态表视图细胞的一个项目,在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];

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

708
投票

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

该方法不涉及定义和创建自己的自定义视图。在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/

迅速四分之三

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

3
投票

我觉得这个代码是没有那么糟糕。

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
}

2
投票

在iOS系统7.0.4我创建了它自己的XIB自定义标题。这里所说的没有工作过。它必须是UITableViewHeaderFooterView的子类与dequeueReusableHeaderFooterViewWithIdentifier:工作,似乎类是很固执的有关背景色。所以最后我添加了一个UIView的(你既可以用代码或IB做)与名customBackgroudView,然后设置它的backgroundColor属性。在layoutSubviews:我设置视图对边界框架。它与iOS 7工作,给无毛刺。

// 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;
}

2
投票

只要改变头部视图层的颜色

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


2
投票

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

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
}

2
投票

我通过控制台日志得到了消息,在Xcode

[TableView中]设置上UITableViewHeaderFooterView背景颜色已被弃用。请您想要的背景颜色为backgroundView属性,而不是设置自定义的UIView。

然后,我只是创建一个新的UIView,奠定其作为HeaderView的背景。没有一个良好的解决方案,但它容易,因为Xcode中说。


2
投票

就我而言,它的工作是这样的:

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

2
投票

刚刚成立的背景视图的背景色:

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

1
投票

随着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

奇迹般有效!


1
投票

斯威夫特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)
    }

或者,如果一个简单的颜色

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

0
投票

虽然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沃金


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

0
投票

使用UIAppearance你可以改变它在你的应用程序是这样所有的标题:

UITableViewHeaderFooterView.appearance()。的backgroundColor = theme.subViewBackgroundColor


50
投票

如果你想自定义颜色的头部,你可以这样做:

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

因为iOS的6.0这个解决方案的伟大工程。


31
投票

下面的解决方案适用于雨燕1.2与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()

}

21
投票

不要忘了从委托添加这段代码或您的观点将被切断或在某些情况下,相对于您的视图/标签的高度的桌子后面出现。

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

18
投票

设置在UITableViewHeaderFooterView背景颜色已被弃用。请改用contentView.backgroundColor


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

17
投票

您可以在2秒左右做它main.storyboard。

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

Have a look here

© www.soinside.com 2019 - 2024. All rights reserved.