自定义UITableView中索引/区域栏的字体/背景颜色

我已经读过另一个线程,你可以在UITableView中添加一个CALayer来调整索引栏(AZ#滚动条)的字体或背景颜色。 我的应用程序将在iOS 5及更高版本上运行,因为在苹果文档中我没有看到任何有关这方面的信息,所以说如果不创build自定义表格视图是不可能的。

如果这是可能的(苹果公司也可以接受),我会怎么做呢?

从iOS 6.0开始,有两种方法可以让您在沿着部分索引拖动时更改部分索引的颜色和显示的背景。

if ([tableView respondsToSelector:@selector(setSectionIndexColor:)]) { tableView.sectionIndexColor = ... // some color tableView.sectionIndexTrackingBackgroundColor = ... // some other color } 

当然,只有当设备具有6.0或更高版本时,才会执行此操作。 在iOS 5.x下,什么都不会改变。

这是swift 2+的代码

 self.tableView.sectionIndexTrackingBackgroundColor = CustomColor // background color while the index view is being touched self.tableView.sectionIndexBackgroundColor = CustomColor // title/text color of index items self.tableView.sectionIndexColor = CustomColor 

对于Swift 3.1:

  self.tblViewForContacts.sectionIndexTrackingBackgroundColor = self.view.backgroundColor self.tblViewForContacts.sectionIndexBackgroundColor = self.view.backgroundColor