自定义UITableView标头设置背景颜色失败 [英] Custom UITableView Header set background colour fail

查看:148
本文介绍了自定义UITableView标头设置背景颜色失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个自定义的UITableViewHeaderFooterView,并且tableview的背景颜色为白色。

I created a custom UITableViewHeaderFooterView, and the tableview's background colour is white.

self.contentView.backgroundColor = UIColor.clearColor()

但是,节标题的背景始终显示为灰色。如何删除灰色背景??

However, the section header's background always appears like gray. How can I remove the gray background??

因为我已经覆盖了UITableView的drawRect函数,所以我希望在标题视图后面显示一些内容。

Since I have override the drawRect func of the UITableView, so I want something to appear behind the header view.

我尝试了以下内容:

a)将UITableView样式更改为Grouped,问题出在了,但是标题不能粘在表格的顶部。

a) Change the UITableView style to Grouped, the problem goes, but the header cannot glued on top of the table.

b)使用节标题标题而不是标题视图

b) Use section header title instead of header view

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String?

标题的背景是透明的,但我有多个标签。

the header's background is transparent, but I have multiple labels.

任何人都可以帮我解决这个问题吗?

Can anyone help me to figure this out?

感谢@Omkar,设置了正确的方法

Thanks to @Omkar, the correct way is set

cell.layer.backgroundColor = UIColor.clearColor().CGColor


推荐答案

您需要设置内容视图的背景颜色以清除颜色,同时还需要设置tableView单元格的背景颜色以清除颜色。将其放在viewForHeaderInSection方法中。然后,您将能够看到设置为tableView的颜色。

You need to set the background colour of the content view to clear color and at the same time you also need to set the background color of the tableView cell to clear color. Place this in your viewForHeaderInSection method. You will then be able to see the color set to the tableView.

    YourCell.contentView.backgroundColor = UIColor.clearColor()
    YourCell.backgroundColor = UIColor.clearColor()

请查找附件中的代码和还有表格视图,其样式在故事板中是简洁的样式。我还添加了运行后的外观图像

Please find the attached image for my code and also the table view whose style is plain style in storyboard. And i have also added the image of how it looks after running

谢谢

这篇关于自定义UITableView标头设置背景颜色失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆