iPhone上TableView分组表格默认的区头背景颜色是什么?

18
我想自定义TableView的段头并保留默认的背景颜色。我使用 (UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section方法。我需要根据设备类型(iPad或iPhone)更改字体大小。为此,请调用以下函数:

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

但是,我手动发现了这些值。
6个回答

33

在iOS7中,默认的表格视图段头背景颜色为

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)

1
由于所有的颜色分量都相同(247/255),更简单的选择是:UIColor(white: 0.97, alpha: 1)其中0.97约等于247/255。 - Nick Ager
3
我希望您能更新此回答以适用于iOS13和暗黑模式。是否有任何标准的UIColor可以代替那些硬编码的值? - Gwendal Roué

5

要更改TableView Header Section的背景颜色,只需添加一行代码即可。在TableView代理方法willDisplayHeaderView中实现:

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

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

}

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)

这是适用于iOS 9及更高版本的。 - Atpl Mohali
RGB 颜色:#e8e9ed - Andre Simon

0
在我的一个应用程序中,我所做的是创建文本,如下所示:
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);

我使用了290作为约束宽度,以在标签两侧提供边框。然后我使用了可伸缩的图像,如下所示:

UITableViewHeaderImage

并将其缩放以适应标题中的文本:

    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];

最后,我创建了一个标签并将其添加为子视图:
    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;

-1

对于iOS 11,我在模拟器上选择了它的颜色。

  • Objective-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)

但是,每当苹果更改颜色时,我们需要进行更改。

如何获取默认的分区头颜色?


-1

十六进制颜色 - #e8e9ed

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

或者您可以使用 #f7f7f7


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接