2012-01-05 95 views
10

我想自定义TableView节标题并保留默认背景色。我使用 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)部分。我需要根据设备(iPad或iPhone)更改字体大小。为此目的调用下一个功能:iPhone上的默认TableView节标题背景颜色是什么?

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

但我手动发现这些值。

回答

23

为ios7默认的tableview节头的背景颜色是

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) 
+0

作为所有的颜色组件是相同的(247/255),更简单的修改原生是: 'UIColor(白色:0.97,alpha:1)' 其中0.97〜247/255 – 2017-03-15 10:20:31

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

对于TableView中报头部分的更改背景颜色只有一行的东西 添加TableView委托willDisplayHeaderView

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

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

} 
1

的Objective-C:

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

斯威夫特:

UIColor(red: 232/255, green: 233/255, blue: 237/255, alpha: 1) 
+0

这适用于iOS 9及更高版本 – 2017-07-15 10:31:27