5

How do I set a transparent background for the sections index bar in a UITableView?

I've tried:

tableView.sectionIndexBackgroundColor = .clearColor()

but the bar gets a light grey background

EDIT I've tried to change background color of the tableview but it doesn't seem to influence.

  tableView.backgroundView = UIView.init()
  tableView.backgroundColor = .blackColor() // tried .clearColor() too.

EDIT2

  tableView.sectionIndexBackgroundColor = UIColor(white: CGFloat(1.0), alpha: CGFloat(0.5)) // change the alpha and check once

I've also tried a color with alpha values. I can make the background semi-transparent (see line code above). But if I set alpha to 0.0 to make it completely transparent, I get again the light grey background.

aneuryzm
  • 63,052
  • 100
  • 273
  • 488

2 Answers2

0

tableView.sectionIndexBackgroundColor = .clear works perfect here's example

Damir Shakenov
  • 331
  • 4
  • 17
0

For swift 4.2 you must set viewBackground for your section and for that view set background color.

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

Note: Init just shortcut for initializing elements.

Andrey
  • 51
  • 1
  • 2