Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView background color does not change

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

like image 686
Robin Avatar asked Jan 24 '11 18:01

Robin


People also ask

How do I change the background color of a table in Swift?

Basic Swift Code for iOS Apps For changing the background color of the table view cell, you should change the contentView. backgroundColor property of the cell. Now run the project to see the effect.


2 Answers

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.

like image 110
John Parker Avatar answered Sep 22 '22 17:09

John Parker


Make sure you have the following 3 options set:

tbView.opaque = NO;
tbView.backgroundColor = [UIColor clearColor];
tbView.backgroundView  = nil;
like image 41
WrightsCS Avatar answered Sep 23 '22 17:09

WrightsCS