What is the default TableView section header background color on the iPhone?
Asked Answered
S

6

18

I want to customize TableView section header and to leave default background color. I use - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section. I need to change font size depending on device (iPad or iPhone). For this purpose call next function:

[UIColor colorWithHue:0.6 saturation:0.33 brightness: 0.69 alpha:0.6].

But I found these values manually.

Scintillator answered 5/1, 2012 at 12:28 Comment(0)
A
33

The default tableview section header background color for ios7 is

Objective-C
[UIColor colorWithRed:247/255.0f green:247/255.0f blue:247/255.0f alpha:1.0f]

Swift
UIColor(red: 247/255, green: 247/255, blue: 247/255, alpha: 1)
Anya answered 11/7, 2014 at 18:53 Comment(2)
As all the colour components are identical (247/255), a simpler alternative is: UIColor(white: 0.97, alpha: 1) where 0.97 ~ 247/255Kamchatka
I wish this answer would be updated for iOS13 and the dark mode. Is there any standard UIColor that we could use instead of those hard-coded values?Begga
C
5

For Change background Color of TableView Header Section Just One Line stuff add TableView delegate willDisplayHeaderView:

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {

   UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
   **header.tintColor = [UIColor whiteColor];**

}
Cattan answered 24/10, 2016 at 6:19 Comment(0)
M
4

Objective-C:

[UIColor colorWithRed:232/255.0f green:233/255.0f blue:237/255.0f alpha:1.0f]

Swift:

UIColor(red: 232/255, green: 233/255, blue: 237/255, alpha: 1)
Magyar answered 15/7, 2017 at 10:30 Comment(2)
This is for iOS 9 and laterMagyar
RGB color: #e8e9edMatthews
D
0

What I've done in one of my apps is to create the text like so:

NSString *sectionTitle = @"YOUR TITLE HERE";
CGSize sz = [sectionTitle  sizeWithFont:[UIFont boldSystemFontOfSize:20.0f] 
                   constrainedToSize:CGSizeMake(290.0f, 20000.0f)
                       lineBreakMode:UILineBreakModeWordWrap];

CGFloat height = MAX(sz.height, 20.0f);

CGRect sectionFrame = CGRectMake(0.0, 0.0, 320.0, height);

I used 290 for the constraint width to give the label boarders on either side. I then used a stretchable image like this:

UITableViewHeaderImage

And scaled it to fit the text in the header:

    UIImage *headerImage = [UIImage imageNamed:@"sectionheaderbackground.png"];
    UIImage *stretchableImage = [headerImage stretchableImageWithLeftCapWidth:12 topCapHeight:0];
    UIImageView *backgroundImageView = [[UIImageView alloc] initWithFrame:sectionFrame];
    backgroundImageView.image = stretchableImage;

    // Add it to the view for the header
    UIView *sectionView = [[UIView alloc] initWithFrame:sectionFrame];
    sectionView.alpha = 0.9; 
    sectionView.backgroundColor = [UIColor clearColor];
    [sectionView addSubview:backgroundImageView];

Finally, I created a label and added it as a subview:

    CGRect labelFrame = CGRectMake(10.0, 0.0, 290.0, height);
    UILabel *sectionLabel = [[UILabel alloc] initWithFrame:labelFrame];
    sectionLabel.text = sectionTitle;
    sectionLabel.numberOfLines = 0;
    sectionLabel.font = [UIFont boldSystemFontOfSize:18.0];
    sectionLabel.textColor = [UIColor whiteColor];
    sectionLabel.shadowColor = [UIColor grayColor];
    sectionLabel.shadowOffset = CGSizeMake(0, 1);
    sectionLabel.backgroundColor = [UIColor clearColor];
    [sectionView addSubview:sectionLabel];

    return sectionView;
Downgrade answered 15/12, 2012 at 13:27 Comment(0)
S
-1

for iOS 11, I picked it's color on simulator.

  • object c: [UIColor colorWithRed:247/255.0 green:247/255.0 blue:247/255.0 alpha:1.0]

  • swift : UIColor(red: 247/255.0, green: 247/255.0, blue: 247/255.0, alpha: 1.0)

But, we need to change whenever Apple changed the color.

How to get default section header color?

Stanwinn answered 19/8, 2018 at 11:26 Comment(0)
P
-1

Hex color - #e8e9ed

RGB color - UIColor(red: 232/255, green: 233/255, blue: 237/255, alpha: 1)

or you can use #f7f7f7

Post answered 20/7, 2019 at 6:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.