19

In a model UIViewController I have the following implementation of loadView (everything is created programmatically):

- (void)loadView {

    // Add Basic View
    UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 540, 620)];
    myView.backgroundColor = [UIColor clearColor];
    self.view = myView;
    [myView release];

    // Add NavigationBar

    // Add a BG image

    // Add Table
    UITableView *tbView = [[UITableView alloc] initWithFrame:CGRectMake(30, 80, 480, 250) style:UITableViewStyleGrouped];
    tbView.dataSource = self;
    tbView.delegate = self;
    tbView.scrollEnabled = NO;
    tbView.backgroundColor = [UIColor clearColor];
    [tbView reloadData];

    [self.view addSubview:tbView];
    [tbView release];

    // some more code
}

As you can see I set backgroundColor to clearColor, yet when I compile and run the code I always see a gray background behind the table: enter image description here

I don't understand what I am doing wrong (sounds stupid, I know), I used to have the very same code and it worked perfectly fine. I am compiling with iOS SDK 4.2.1

Robin
  • 8,197
  • 11
  • 45
  • 74
  • You shouldn't be doing all this setup in `loadView`, it should be in `viewDidLoad`. In `loadView` you should *just* create the view, not set colors, etc. Its ok to set the frame to `CGRectZero` in `loadView`. You can change it later in `viewDidLoad`. – Robotbugs Mar 19 '13 at 04:45

4 Answers4

45

You also need to set your UITableView's backgroundView property to nil on recent (since 3.2) versions of iOS.

As such, adding...

tbView.backgroundView = nil;

...should sort your problems.

That said, if you want to maintain compatibilty with pre-3.2 devices, you should check for the existence of this via the instancesRespondToSelector method before calling it.

John Parker
  • 54,048
  • 11
  • 129
  • 129
  • that worked, thanks! I don't quite understand why they added this view though, what would be the difference to just adding the UITableView as a subview of any custom view? – Robin Jan 24 '11 at 18:40
  • @Robin I suppose it's a cleaner path if the UITableView is effectively your top level view, but that's pretty much it. – John Parker Jan 24 '11 at 18:42
  • It's weird that I never had to do this until just recently. +1 – James Sep 19 '13 at 17:38
14

Make sure you have the following 3 options set:

tbView.opaque = NO;
tbView.backgroundColor = [UIColor clearColor];
tbView.backgroundView  = nil;
WrightsCS
  • 50,551
  • 22
  • 134
  • 186
  • 3
    This worked for me, but I had to put the code in the `viewWillAppear`. It did not work if I put it in the `viewDidLoad`. – Guruniverse Jun 15 '16 at 21:55
  • @Guruniverse That response is what fixed my problem after over an hour of searching. Thank you! – kmell96 Mar 28 '18 at 18:23
3

try

tbView.backgroundView = nil;
vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
1

I tried to change from storyboard. It works fine in

tbView.backgroundColor = .white
Osadhi Virochana
  • 1,294
  • 2
  • 11
  • 21