22

I want to make the grouped UITableView transparent. I partially succeded with the following code:

UIColor *bgColor = [[UIColor alloc] initWithWhite:1 alpha:0.0];
historyTable.backgroundColor = bgColor;

Unfortunately, black corners appeared in the rounded cells. How to get rid of them?

Before After

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Jacek
  • 1,894
  • 5
  • 21
  • 34

4 Answers4

37

Instead of using

UIColor *bgColor = [[UIColor alloc] initWithWhite:1 alpha:0.0];
historyTable.backgroundColor = bgColor;

Just use:

historyTable.backgroundColor = [UIColor clearColor];

That also clears up the memory leak you were creating.

Tom Irving
  • 10,041
  • 6
  • 47
  • 63
  • 1
    Thank you very much. BTW I relese bgColor later on, but thanks for your concern ;). – Jacek Mar 07 '10 at 12:50
  • nice one.. just what i needed today – Scrimmers Aug 27 '10 at 11:22
  • 6
    Note: [UIColor clearColor] must currently be set in code, it doesn't work if the clearColor is set in Interface Builder. – Sascha Konietzke Jan 07 '11 at 08:57
  • Didn't know that; I never use IB. It might be worth submitting a bug report if this is the case. – Tom Irving Jan 07 '11 at 12:59
  • Yeah, try using a color with a certain alpha value above zero. That said, however, I'm not sure how'd you deal with the black corners (if they appear). – Tom Irving May 30 '11 at 20:52
  • 1
    By the way, `[UIColor colorWithWhite:0 alpha:0]` also works, as that is what `[UIColor clearColor]` is really equal to. Due to some oversight, colors with alpha 0 but different RGB values are not equal. – user102008 Mar 01 '12 at 05:11
35

remove UITableView backgroundView

xxx.backgroundView = nil;

This is necessary on iPad builds. When compiling to run on iPad and iPhone, check the tableView responds to the selector with ...

if ([self.tableView respondsToSelector:@selector(setBackgroundView:)]) {
    [self.tableView setBackgroundView:nil];
}
Mathew Waters
  • 486
  • 4
  • 6
021850524
  • 351
  • 3
  • 2
  • setBackgroundView: available from iOS 3.2 and not pinned to iPad, isn't it? Why one should use this check? – adruzh Mar 16 '12 at 10:28
8

for me it worked finaly after setting both to nil/clear:

[myTableView setBackgroundView:nil];
[myTableView setBackgroundColor:[UIColor clearColor]];
zero3nna
  • 2,770
  • 30
  • 28
3

I had this issue and found that there was no difference between using:

[[UIColor alloc] initWithWhite:1 alpha:0.0];

and using:

[UIColor clearColor];

I tried both of these and still had the little black corners on my table view.

I also tried setting the backgroundView to nil as suggested, but this didn't work either.

I solved this by setting the backgrounds of the individual cells to transparent in the cellForRowAtIndexPath method:

cell.backgroundColor =  [UIColor clearColor];

Of course, this has the side effect that your cells themselves are transparent, which isn't ideal for everyone, but it ok for me in this case.

Pete McPhearson
  • 469
  • 1
  • 7
  • 17