简体   繁体   中英

tableViewheader transparent background becomes black

I've cleared the background of the tableview. Now I've created a PNG image with a transparent background and set it as the backgroundView of the tableViewHeader.

Somehow the tranparent background of the PNG becomes black in the tableViewHeader. Does anybody has any clue howcome?

Thank you.

//Header Layout
UIView *headerView =
[[[UIView alloc]
  initWithFrame:CGRectMake(0, 0, 300, 70)]
 autorelease];
UILabel *headerLabel =
[[[UILabel alloc]
  initWithFrame:CGRectMake(10, 10, 300, 40)]
 autorelease];
headerView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"headerBG.png"]];
headerLabel.text = NSLocalizedString(@"Huidig", @"");
headerLabel.textColor = [UIColor redColor];
headerLabel.shadowColor = [UIColor greenColor];
headerLabel.shadowOffset = CGSizeMake(0, 1);
headerLabel.font = [UIFont boldSystemFontOfSize:22];
headerLabel.backgroundColor = [UIColor clearColor];
[headerView addSubview:headerLabel];
tableView.tableHeaderView = headerView;

have you tried

[tableView setBackgroundColor:[UIColor clearColor]];
[tableView setBackgroundView:nil];

I faced a similar problem when having a background image for a UILabel using [UIColor colorWithPatternImage:]. A device running 4.2.1 (iPhone 3G) displayed the transparent part of the image black. I have tried changing the opaque property and also setting the image to the layer, but both didn't work.

Instead I implemented a new class inheriting UILabel, and in the drawRect I just draw the image:

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextClearRect(context, rect);

    UIImage *image = [UIImage imageNamed:@"graphics/pricetag.png"];
    CGContextDrawImage(context, rect, image.CGImage);
    // Have UILabel draw the text
    [super drawRect:rect];
}

I tried your code and works absolutely fine for me. Please check your tableview one more time. If you still see the problem please clean (shift+commond+K) it and compile it one more time or check your image one more time. FInal option clear your tableview one more time.

    tableView.backgroundColor = [UIColor clearColor];

Instead of using +colorWithPatternImage try setting the contents of headerView 's backing CALayer to your image. You'll need to link against and import <QuartzCore/QuartzCore.h> for this approach.

headerView.layer.contents = [UIImage imageNamed:@"headerBG"].CGImage;

tried that???

[tableView setBackgroundColor:[UIColor clearColor]];
[tableView setBackgroundView:nil];

and in your

-(void)viewDidAppear{
 ....................
 ....................
[tableview reloadData];
}

May be it will work now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM