user542584
user542584

Reputation: 2699

UITableview background view colour -iphone

I have an UITableview controller without an XIB. I set the background colour of the tableview by doing this:

[self.tableView setBackgroundColor:[UIColor colorWithPatternImage:
                                    [UIImage imageNamed:@"MmyImage.png"]]];

My image is a gradient image, so eventhough the above code is ok, it redraws the image for each cell when the tableview goes to edit view, and it appears as lines which is not looking elegant.

I would like to set the tableviews background to clear colour and set the superview's colour to the image, so that the tableview transitions smoothly over the superview. However the below code does not work:

[self.view setBackgroundColor:[UIColor colorWithPatternImage:
                               [UIImage imageNamed:@"myImage.png"]]];
[self.tableView setBackgroundColor:[UIColor clearColor]];

This makes the background completely white, I dont know why.

Help appreciated..thanks.

Edited:new code but this does not work as well:

   UIImageView * imgBg = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"myImage.png"] stretchableImageWithLeftCapWidth:6 topCapHeight:6]];
     imgBg.frame = self.tableView.window.bounds;
    [self.tableView.superview addSubview:imgBg];
//[self.view addSubview:imgBg];
    //[self.tableView.window sendSubviewToBack:imgBg];
    //[self.view  bringSubviewToFront:self.tableView];
    [self.tableView setBackgroundColor:[UIColor clearColor]];
    [imgBg release];

Upvotes: 2

Views: 3593

Answers (3)

Roki
Roki

Reputation: 2688

then your second code will be right, except:

[self.view setBackgroundColor:[UIColor colorWithPatternImage:
                               [UIImage imageNamed:@"myImage.png"]]];

replace this:

UIImageView * imgBg = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"myImage.png"] stretchableImageWithLeftCapWidth:6 topCapHeight:6]];
imgBg.frame = self.view.bounds;
[self.view addSubview:imgBg];
[self.view sendSubviewToBack:imgBg];

Edit I think: [self.tableView setBackgroundColor:[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0]]; will helps, that is the only difference now.

Upvotes: 0

Deepak Danduprolu
Deepak Danduprolu

Reputation: 44633

In a custom UITableViewController subclass, self.view will return the same as self.tableView. So what you're doing there is altering the same object. In the end, you get to see the UIWindow. So to get the desired result, you should do this –

[self.view.window setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"myImage.png"]]];
[self.tableView setBackgroundColor:[UIColor clearColor]];

UITableView has a backgroundView property. Use it.

aTableView.backgroundView = [[[UIView alloc] initWithFrame:aTableView.bounds] autorelease];
aTableView.backgroundView.backgroundColor = backgroundColor;

Upvotes: 6

karim
karim

Reputation: 15599

First in your xib file, add a parent view. add table view in that parent view. set parent view's background color to your desired background. then set your tableview's background color to [UIColor clearColor].

Check also, if your table view is a group table style or not? if it is group table style, then the cell have distinct background. Then you need to clear the background of each cell. The best way to do is,

  1. create a blank UIView *bkview = [[UIView alloc] initWithRect:CGRect(0,0)];

  2. set this as your cells background view.

I hope this will work.

Upvotes: 0

Related Questions