How to change font color of section index titles
I want to display an indexed table view.
B开发者_运维技巧y default, section index titles have gray color.
How do I change their color?
EDIT/FOLLOW UP iOS7 (Sept. 2013) Read answer of Graham Perks for a follow up on iOS7 where he explains about sectionIndexTrackingBackgroundColor
and sectionIndexBackgroundColor
Follow up (Sept. 2012) of this question for future readers.
Since iOS 6 it is now possible to change the index color text and background. Two functions are available for that (see iOS 6 documentation).
sectionIndexColor
The color to use for the table view’s index text.
@property(nonatomic, retain) UIColor *sectionIndexColor
Table views can display an index along the side of the view, making it easier for users to navigate the contents of the table quickly. This property specifies the color to use for text displayed in this region.
Availability : Available in iOS 6.0 and later.
Declared In : UITableView.h
sectionIndexTrackingBackgroundColor
The color to use for the table view’s index background area.
@property(nonatomic, retain) UIColor *sectionIndexTrackingBackgroundColor
Table views can display an index along the side of the view, making it easier for users to navigate the contents of the table quickly. This property specifies the color to display in the background of the index when the user drags a finger through it.
Availability : Available in iOS 6.0 and later.
Declared In : UITableView.h
Add the below code in your viewDidLoad
(for swift):
tableView.sectionIndexColor = UIColor.lightGrayColor()
In iOS 7, to change the normal background color use the sectionIndexBackgroundColor
property.
sectionIndexTrackingBackgroundColor
is the background color while tracking with your finger.
(Thanks to HpTerm for the initial pointers which work great on iOS 6.)
optimized way to change index section background color and text color
- (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"]]];
}
I think i read the wrong question just now.
if you mean the A-Z index that let you navigate the section title of the tableview, then it cannot be change,not possible for the moment. Or you can create your own.
FOLLOW UP
This only apply to before iOS6. For iOS6 and above please refer to HpTerm's answer.
The possible duplicate here indicates an "undocumented" way of doing it. I used it in production code, it works (not tested for iOS under 5.0).
Sorry you can not change the color of the index.
Sorry, a bit late, but for everyone who's searching for an answer, here the Swift code
for view in tableView.subviews {
if view.respondsToSelector("setIndexColor:") {
view.performSelector("setIndexColor:", withObject: UIColor.yourColor())
}
}
Then if you want to get the section index as view and do what you want with it, here it is:
for view in tableView.subviews {
NSStringFromClass(view.classForCoder) == "UITableViewIndex" {
// do what you want
// e.g: view.hidden = true
}
}
Simply change table view tint colour to your desired colour.
精彩评论