简体   繁体   中英

How do I set a transparent background for the sections Index Bar in a UITableView?

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.

tableView.sectionIndexBackgroundColor = .clear works perfect 这是一个例子

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM