How to recreate the default UIView the same as the default tableView:viewForHeaderInSection:?

后端 未结 4 657
面向向阳花
面向向阳花 2021-02-04 16:58

I tried to implement the

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

to get the text label of the

4条回答
  •  感动是毒
    2021-02-04 17:36

    Although this is not exactly right, I'm posting this in hopes that someone can improve upon my approach. The text is right (white like default, but you can change that). The background gradient and opacity are not perfect (too light??)--some tweaking here might be needed.

    Get the sectionheaderbackground.png here: http://img5.imageshack.us/img5/6616/sectionheaderbackground.png

    // Create a stretchable image that emulates the default gradient
    UIImage *buttonImageNormal = [UIImage imageNamed:@"sectionheaderbackground.png"];
    UIImage *stretchableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:12 topCapHeight:0];
    
    // Create the view for the header
    CGRect sectionFrame = CGRectMake(0.0, 0.0, 320.0, 22.0);
    UIView *sectionView = [[UIView alloc] initWithFrame:sectionFrame];
    sectionView.alpha = 0.9;
    sectionView.backgroundColor = [UIColor colorWithPatternImage:stretchableButtonImageNormal];
    
    // Create the label
    CGRect labelFrame = CGRectMake(10.0, 0.0, 310.0, 22.0);
    UILabel *sectionLabel = [[UILabel alloc] initWithFrame:labelFrame];
    sectionLabel.text = @"Test section label";
    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];
    [sectionLabel release];
    
    // Return the header section view
    return sectionView;
    

提交回复
热议问题