我有一个带有字母索引的表格视图,并且在使用侧面字母表快速浏览列表。 对于那些不熟悉的人,使用的方法如下:
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
我的问题是,我的应用程序刚被改成黑色皮肤。所以现在很难看到侧边的字母表。
我无法找出如何更改字母表的颜色。如果可能的话,我想把它改为白色。
我有一个带有字母索引的表格视图,并且在使用侧面字母表快速浏览列表。 对于那些不熟悉的人,使用的方法如下:
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
我的问题是,我的应用程序刚被改成黑色皮肤。所以现在很难看到侧边的字母表。
我无法找出如何更改字母表的颜色。如果可能的话,我想把它改为白色。
UITableView
的sectionIndexColor
属性来设置表视图索引文本的颜色。- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {
for(UIView *view in [tv subviews])
{
if([[[view class] description] isEqualToString:@"UITableViewIndex"])
{
[view setBackgroundColor:[UIColor whiteColor]];
[view setFont:[UIFont systemFontOfSize:14]];
}
}
//rest of cellForRow handling...
}
这说明了如何访问和修改UITableViewIndex视图的一些方面。看起来该视图没有任何子视图,因此可能正在使用索引标题数组进行自定义绘制。
虽然不完美,但希望能有所帮助。
保罗
这个可以在UITableView的界面生成器中轻松更改 - 无需使用未记录的类?
如下所示的屏幕截图,你可以看到字体颜色和背景颜色也可以改变。工作完成了!
对于 iOS7 或更高版本:
self.tableView.sectionIndexColor = [UIColor whiteColor];
self.tableView.sectionIndexBackgroundColor = [UIColor clearColor];
我们已经成功使用了以下代码:
for(UIView *view in [tableView subviews]) {
if([view respondsToSelector:@selector(setIndexColor:)]) {
[view performSelector:@selector(setIndexColor:) withObject:[UIColor whiteColor]];
}
}
这个方法运行良好,它与Mooglas的答案非常相似,但避免使用"UITableViewIndex"这个词。
我之前遇到了同样的问题, 但现在我找到了一种解决方法,但是这种方法使用了未记录的类和方法,所以在尝试上传到App Store之前请三思。我应该说明一下,我只在iOS5上尝试过这个方法,不知道它是否适用于之前的版本。
我借鉴并修改了Erica Saunders的菜谱示例,使其现在将文本颜色更改为红色:
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {
for(UIView *view in [tv subviews])
{
if([[[view class] description] isEqualToString:@"UITableViewIndex"])
{
[view performSelector:@selector(setIndexColor:) withObject:[UIColor redColor]];
}
}
//rest of cellForRow handling...
}
[[UITableView appearance] setTintColor:[UIColor purpleColor]];
Swift 5:
tableView.sectionIndexColor = .red
tableView.sectionIndexBackgroundColor = .clear
有一种方法可以更改索引字母的颜色。
在您的头文件中,将UITableView声明为属性:
@property (weak, nonatomic) IBOutlet UITableView *mainTable;
然后在你的实现文件的viewDidAppear方法中,使用以下代码:
//Change the color of the UITableView index color
_mainTable.sectionIndexColor = [UIColor blackColor];
这是我找到的调整侧边索引栏背景颜色的最佳解决方案。它在iOS7和可能的iOS6中都有效。
将以下内容添加到您的viewDidLoad函数中:
if ([_tableView respondsToSelector:@selector(setSectionIndexColor:)]) {
_tableView.sectionIndexBackgroundColor = [UIColor clearColor];
_tableView.sectionIndexTrackingBackgroundColor = [UIColor whiteColor];
}