如何为UITableView的索引栏设置透明背景?

5
如何为UITableView中的sections index bar设置透明背景?
我已经尝试过:
tableView.sectionIndexBackgroundColor = .clearColor()

但是导航栏会有浅灰色的背景。 编辑 我尝试更改表视图的背景颜色,但好像没有影响。
  tableView.backgroundView = UIView.init()
  tableView.backgroundColor = .blackColor() // tried .clearColor() too.

"EDIT2" 的 HTML 标记。
  tableView.sectionIndexBackgroundColor = UIColor(white: CGFloat(1.0), alpha: CGFloat(0.5)) // change the alpha and check once

我也尝试了一个带alpha值的颜色。我可以使背景半透明(见上面的代码行)。但如果我将alpha设为0.0以完全透明,我再次得到浅灰色的背景。

1
表格的背景色是什么? - Fahri Azimov
@FahriAzimov,我已经添加了我的问题的答案。 - aneuryzm
使用tableViewCell作为表头部分,或者使用透明背景的视图。默认情况下,表格的区域标题具有浅灰色。 - Md. Ibrahim Hassan
使用自定义索引栏来为表视图添加索引怎么样?就像这个:https://github.com/gdavis/GDIIndexBar - Fahri Azimov
@FahriAzimov,我宁愿不改变项目中的代码来实现这个小改变。使用原生的iOS API不能使索引栏具有透明背景吗? - aneuryzm
2个回答

0

对于Swift 4.2,您必须为您的部分设置"viewBackground",并为该视图设置背景颜色。

class CommonSection: UITableViewHeaderFooterView { static let lightSectionColor = UIColor.gray.withAlphaComponent(0.005) func setup(title: String, sectionColor: UIColor? = nil) { textLabel?.text = title textLabel?.font = UIFont.preferredFont(forTextStyle: .largeTitle)

guard let color = sectionColor else { return }

self.backgroundView = Init(value: UIView()) {
  $0.backgroundColor = color
}   } }
注:Init 只是初始化元素的快捷方式。

0

tableView.sectionIndexBackgroundColor = .clear非常完美地工作了这里是示例


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接