thndrkiss
thndrkiss

Reputation: 4595

change background color of a section header title (normal tableview)

My app looks just like the default address book app on iphone. For each alphabet there is a section header in the address book( A, B . .) in gray gradient background color. Is it possible to override that default color to some other tint color ?

Upvotes: 1

Views: 4673

Answers (2)

binshi
binshi

Reputation: 1328

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    // create the parent view that will hold header Label
    UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 25.0)];

    // create the button object
    UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    headerLabel.backgroundColor = [UIColor blackColor];
    headerLabel.opaque = NO;
    headerLabel.textColor = [UIColor whiteColor];
    headerLabel.highlightedTextColor = [UIColor whiteColor];
    headerLabel.font = [UIFont boldSystemFontOfSize:20];
    headerLabel.frame = CGRectMake(0.0, 0.0, 320.0, 22.0);

    // If you want to align the header text as centered
    //headerLabel.frame = CGRectMake(150.0, 0.0, 300.0, 44.0);

    headerLabel.text = @"Name of header";

    [customView addSubview:headerLabel];

    return customView;

}

Upvotes: 4

xmr
xmr

Reputation: 635

You can replace the section headers of a UITableView by implementing the following method in the UITableViewDelegate:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

You will probably also want to implement the height method to ensure your new section view displays correctly :

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

To change the colour of the section header, create a UIView with a UIImageView within it, and a UILabel for the text of the section. Add an image to the UIImageView for how you want the background to look.

AFAIK, you can't manipulate the tintColor of the section view.

Upvotes: 1

Related Questions