ios - iOS:为 tableView 和 tableHeaderView 制作不同的背景颜色
问题描述
这是我要存档的内容
- 我的主视图有渐变的红色。
- tableHeaderView 应该有一个清晰的颜色(因为它是一个圆角的视图,所以在某些部分应该清楚显示圆角)。
- tableView(除了标题)应该有一个白色的颜色。即使它只有一个单元格,该单元格下方的底部空间也应该是白色的。
我的视图层次结构很简单:我在主视图之上有一个 tableView
view.backgroundColor = .clear
tableView.backgroundColor = UIColor.clear
...
cell.backgroundColor = UIColor.clear
...
tableView.tableHeaderView = someView (with clear color)
所以,在设置之后,我有一个背景颜色清晰的表格视图,如果我有一个单元格(我将其背景颜色设置为白色),那么该单元格下方的空间是清晰的。但它应该是白色的。我怎么能得到那个?
- - 解决了:
tableView.tableFooterView = UIView()
if let tableFooterView = tableView.tableFooterView {
let bigFooterView = UIView()
bigFooterView.backgroundColor = .white
tableFooterView.addSubview(bigFooterView)
bigFooterView.translatesAutoresizingMaskIntoConstraints = false
tableFooterView.addConstraint(NSLayoutConstraint(item: bigFooterView, attribute: .trailing, relatedBy: .equal, toItem: tableFooterView, attribute: .trailing, multiplier: 1, constant: 0))
tableFooterView.addConstraint(NSLayoutConstraint(item: bigFooterView, attribute: .leading, relatedBy: .equal, toItem: tableFooterView, attribute: .leading, multiplier: 1, constant: 0))
tableFooterView.addConstraint(NSLayoutConstraint(item: bigFooterView, attribute: .top, relatedBy: .equal, toItem: tableFooterView, attribute: .top, multiplier: 1, constant: 0))
tableFooterView.addConstraint(NSLayoutConstraint(item: bigFooterView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: UIScreen.main.bounds.height*2))
}
解决方案
将表格页脚设置为白色视图。
让 footerView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: someHeight) footerView.backgroundColor = .white
tableView.tableFooterView = 页脚视图
推荐阅读
- c# - Windbg 从 .foreach 值解析变量
- c++ - 我收到以下错误:在抛出 'std::bad_alloc' 的实例后调用终止
- python - /personal_detail/ 处的 IntegrityError
- reactjs - 自定义反应表
“全选”复选框列位置和样式 - swift - 在 Swift 中,如何在嵌套计时器中引用“self”?
- r - 以公里为单位对具有给定分辨率的矢量进行栅格化
- c++ - 如何在 C++ 中更改我的连接图,以便每个顶点都可以容纳一个对象?
- froala - 如何从 Froala 编辑器中新生成的 P 标签中删除样式?
- flutter - 如何在颤振中应用 flex
- python - 包含子键和子值的 JSON 文件