UITableViewHeaderFooterView:无法更改背景颜色

Chu*_*hun 111 iphone xcode uitableview ios

我正在尝试更改UITableViewHeaderFooterView的背景颜色.虽然视图正在显示,但背景颜色仍然是默认颜色.我从xcode获取日志说:

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

但是,以下选项均不起作用:

myTableViewHeaderFooterView.contentView.backgroundColor = [UIColor blackColor];
myTableViewHeaderFooterView.backgroundView.backgroundColor = [UIColor blackColor];
myTableViewHeaderFooterView.backgroundColor = [UIColor blackColor];
Run Code Online (Sandbox Code Playgroud)

我也尝试在xib文件中更改视图的背景颜色.

有什么建议?谢谢.

Swi*_*ect 182

iOS 8,9,10,11 ......

设置任何颜色(使用任何alpha)的唯一方法是使用backgroundView:

迅速

self.backgroundView = UIView(frame: self.bounds)
self.backgroundView.backgroundColor = UIColor(white: 0.5, alpha: 0.5)
Run Code Online (Sandbox Code Playgroud)

OBJ-C

self.backgroundView = ({
    UIView * view = [[UIView alloc] initWithFrame:self.bounds];
    view.backgroundColor = [UIColor colorWithWhite: 0.5 alpha:0.5];
    view;
    });
Run Code Online (Sandbox Code Playgroud)

回应评论

  • 这些其他选项都不可靠地工作(尽管下面的评论)

    // self.contentView.backgroundColor = [UIColor clearColor];
    // self.backgroundColor = [UIColor clearColor];
    // self.tintColor = [UIColor clearColor];
    
    Run Code Online (Sandbox Code Playgroud)
  • backgroundView会自动调整大小.(无需添加约束)

  • 使用UIColor(white: 0.5, alpha: 0.5)或控制alpha backgroundView.alpha = 0.5.
    (当然,任何颜色都可以)

  • 使用XIB时,请创建根视图a UITableViewHeaderFooterView并以backgroundView编程方式关联:

    注册:

    tableView.register(UINib(nibName: "View", bundle: nil),
                       forHeaderFooterViewReuseIdentifier: "header")
    
    Run Code Online (Sandbox Code Playgroud)

    加载:

    override func tableView(_ tableView: UITableView,
                            viewForHeaderInSection section: Int) -> UIView? {
        if let header =
            tableView.dequeueReusableHeaderFooterView(withIdentifier: "header") {
            let backgroundView = UIView(frame: header.bounds)
            backgroundView.backgroundColor = UIColor(white: 0.5, alpha: 0.5)
            header.backgroundView = backgroundView
            return header
        }
        return nil
    }
    
    Run Code Online (Sandbox Code Playgroud)

演示

LinkedIn重播动画

►在GitHub上找到此解决方案以及有关Swift Recipes的其他详细信息.

  • 对于仍然收到警告的人:确保UITableViewHeaderFooterView没有在IB中设置backgroundColor. (7认同)
  • contentView不存在,这就是原因.tintcolor是为视图着色而不是它的背景.和backgroundcolor已弃用.因此,您的方式是使其工作的方式.顺便说一下很好的代码语法 (5认同)

Mat*_*att 80

您应该使用myTableViewHeaderFooterView.tintColor,或者为myTableViewHeaderFooterView.backgroundView指定自定义背景视图.

  • 不知道为什么,但对我来说`tintColor`不适用于iOS7.我只能通过分配自定义视图来改变颜色:`myTableViewHeaderFooterView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"myImage.png"]];` (15认同)
  • TintColor不是背景色!! (7认同)
  • myTableViewHeaderFooterView.tintColor 工作完美。谢谢你的回答! (4认同)

sas*_*ash 21

在iOS 7中contentView.backgroundColor为我工作,tintColor没有.

   headerView.contentView.backgroundColor = [UIColor blackColor];
Run Code Online (Sandbox Code Playgroud)

虽然clearColor对我不起作用,我找到的解决方案是将backgroundView属性设置 为透明图像.也许它会帮助某人:

UIGraphicsBeginImageContextWithOptions(CGSizeMake(1, 1), NO, 0.0);
UIImage *blank = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

headerView.backgroundView = [[UIImageView alloc] initWithImage:blank];
Run Code Online (Sandbox Code Playgroud)


Rud*_*f J 13

请确保您设置的的的backgroundColor contentViewUITableViewHeaderFooterView:

self.contentView.backgroundColor = [UIColor whiteColor];
Run Code Online (Sandbox Code Playgroud)

然后它会工作.


AmJ*_*mJa 11

对我来说,我尝试了上述所有内容,但仍然收到警告"在UITableViewHeaderFooterView上设置背景颜色已被弃用.请改用contentView.backgroundColor." 然后我尝试了这个:在xib文件中,标题视图的背景颜色被选中以清除颜色而不是默认颜色,一旦我将其更改为默认警告就消失了. 是这个

改成了这个


Ene*_*nso 11

对于纯色背景,设置contentView.backgroundColor应该就足够了:

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    if let headerView = view as? UITableViewHeaderFooterView {
        headerView.contentView.backgroundColor = .red // Works!
    }
}
Run Code Online (Sandbox Code Playgroud)

对于具有透明度的颜色,包括.clear颜色,这不再有效:

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    if let headerView = view as? UITableViewHeaderFooterView {
        headerView.contentView.backgroundColor = .clear // Does not work 
    }
}
Run Code Online (Sandbox Code Playgroud)

对于完全透明的部分标题,将backgroundView属性设置为空视图:

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    if let headerView = view as? UITableViewHeaderFooterView {
        headerView.backgroundView = UIView() // Works!
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,请注意可能的副作用。除非 table view 设置为“Grouped”,否则当向下滚动时,节标题将在顶部对齐。如果部分标题是透明的,则单元格内容将被看到,这可能看起来不太好。

在这里,节标题具有透明背景:

在此处输入图片说明

为了防止这种情况,最好将部分标题的背景设置为与表视图或视图控制器的背景相匹配的纯色(或渐变)。

在这里,部分标题具有完全不透明的渐变背景:

在此处输入图片说明


Hos*_*sny 8

在此处输入图片说明在界面构建器中,在顶部元素上拉出您的 .xib,在属性检查器中将背景颜色设置为默认值。然后转到内容视图并在那里设置背景颜色(参考https://github.com/jiecao-fm/SwiftTheme/issues/24)。


Chi*_*ndy 6

为了清晰的颜色,我使用

self.contentView.backgroundColor = [UIColor clearColor];
self.backgroundView = [UIView new];
self.backgroundView.backgroundColor = [UIColor clearColor];
Run Code Online (Sandbox Code Playgroud)

对我来说似乎很好.

  • 设置`backgroundView = UIView()` 然后设置`backgroundColor = .clear` 是唯一可行的解​​决方案。谢谢你。 (2认同)