如何更改节索引标题的字体颜色

Abh*_*tra 28 iphone uitableview ios

我想显示一个索引表视图.

默认情况下,部分索引标题具有灰色.

我该如何改变颜色?

HpT*_*erm 73

编辑/关注iOS7(2013年9月)阅读Graham Perks的答案,了解iOS7的后续行动,并向他解释sectionIndexTrackingBackgroundColorsectionIndexBackgroundColor


为未来的读者跟进(2012年9月)这个问题.

从iOS 6开始,现在可以更改索引颜色文本和背景.有两个功能可用(参见iOS 6文档).


sectionIndexColor

用于表视图索引文本的颜色.

@property(nonatomic, retain) UIColor *sectionIndexColor
Run Code Online (Sandbox Code Playgroud)

表视图可以在视图的侧面显示索引,使用户可以更轻松地快速导航表的内容.此属性指定用于此区域中显示的文本的颜色.

可用性:适用于iOS 6.0及更高版本.

声明于:UITableView.h


sectionIndexTrackingBackgroundColor

用于表视图的索引背景区域的颜色.

@property(nonatomic, retain) UIColor *sectionIndexTrackingBackgroundColor
Run Code Online (Sandbox Code Playgroud)

表视图可以在视图的侧面显示索引,使用户可以更轻松地快速导航表的内容.此属性指定当用户拖动手指时在索引背景中显示的颜色.

可用性:适用于iOS 6.0及更高版本.

声明于:UITableView.h

  • `sectionIndexTrackingBackgroundColor`是用手指跟踪时的背景颜色.在iOS7中,要更改正常的背景颜色,请使用`sectionIndexBackgroundColor`. (6认同)

Gra*_*rks 13

iOS 7中,要更改正常的背景颜色,请使用该sectionIndexBackgroundColor属性.

sectionIndexTrackingBackgroundColor是用手指跟踪时的背景颜色.

(感谢HpTerm提供了适用于iOS 6的初始指针.)


小智 9

在你的viewDidLoad(快速)中添加以下代码:

tableView.sectionIndexColor = UIColor.lightGrayColor()
Run Code Online (Sandbox Code Playgroud)


cod*_*cat 5

更改索引部分背景颜色和文本颜色的优化方法

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
    // Background color
    view.tintColor = [UIColor whiteColor];//[UIColor colorWithRed:77.0/255.0 green:162.0/255.0 blue:217.0/255.0 alpha:1.0];
    // Text Color
    UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
    [header.textLabel setTextColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"tableSection"]]];

}
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明


Sol*_*ola 3

我想我刚才读错了问题。

如果您的意思是让您导航表视图的部分标题的 AZ 索引,那么它无法更改,目前不可能。或者您也可以创建自己的。

跟进

这只适用于iOS6之前。iOS6及以上版本请参考HpTerm的回答。

  • +1只是为了支持你。人们不应该对你投反对票。你的答案在 iOS6 之前是正确的,这不值得投反对票。 (4认同)
  • 有可能的。请参阅下面 HpTerm 的回答。 (3认同)