UITableView background color does not change
Asked Answered
B

5

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

Bough answered 24/1, 2011 at 18:31 Comment(1)
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.Ayannaaycock
C
46

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.

Calamity answered 24/1, 2011 at 18:34 Comment(3)
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?Bough
@Bough I suppose it's a cleaner path if the UITableView is effectively your top level view, but that's pretty much it.Calamity
It's weird that I never had to do this until just recently. +1Provencher
F
14

Make sure you have the following 3 options set:

tbView.opaque = NO;
tbView.backgroundColor = [UIColor clearColor];
tbView.backgroundView  = nil;
Fingerling answered 24/1, 2011 at 18:40 Comment(2)
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.Domination
@Domination That response is what fixed my problem after over an hour of searching. Thank you!Abecedarian
I
3

try

tbView.backgroundView = nil;
Inter answered 24/1, 2011 at 18:34 Comment(0)
C
1

I tried to change from storyboard. It works fine in

tbView.backgroundColor = .white
Crass answered 1/2, 2021 at 5:20 Comment(0)
U
0

Swift 5.x I came across this problem recently. The old solution helped (and in my case it was - only in this sequence):

    tableView.backgroundView = nil
    tableView.isOpaque = false
    tableView.backgroundColor = .clear
Untune answered 17/1 at 9:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.