user2633210
user2633210

Reputation: 25

iPhone - UITableView background

I want to give a background image to my UITableView, so I tried this method:

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
UIImageView *v = [[UIImageView alloc] initWithFrame:self.view.bounds];
[v setImage:[UIImage imageNamed:@"Background.png"]];
[self.view addSubview:v];
[self.view sendSubviewToBack: v];
...
}

This works fine. However, after pushViewController, this method doesn't work anymore (why?). So I changed a method:

- (void)viewDidLoad
{
[super loadView];
// Do any additional setup after loading the view from its nib.
self.tableView.backgroundView = nil;
self.tableView.backgroundColor = [[UIColor alloc]initWithPatternImage:[UIImage imageNamed:@"Background.png"]];
...
}

This can work, but the result is not what I expected. Somehow the background picture changes its size (like a partial enlargement), and the background of cells is darker than the previous one.

Anyone knows the reason? I've been stuck with this problem for a long time.

Thanks a lot!

Upvotes: 2

Views: 132

Answers (3)

Shekhar Gupta
Shekhar Gupta

Reputation: 6218

Use this:

UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"TableBackground.jpg"]];
self.tableView.backgroundColor = background;
[background release];

Edit:

UIImage *myImage = [[UIImage alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
myImage.image = [UIImage imageNamed:@"TableBackground.jpg"];
UIColor *background = [[UIColor alloc] initWithPatternImage:myImage];
self.tableView.backgroundColor = background;
[background release];

Upvotes: 3

Prince Kumar Sharma
Prince Kumar Sharma

Reputation: 12641

UITableView *tblView=[[UITableView alloc] init];
[tblView setFrame:CGRectMake(0, 0, 320, 300)];


UIImageView *imgView=[[UIImageView alloc] initWithFrame:tblView.frame];
[imgView setImage:[UIImage imageNamed:@"cinemax.jpg"]];
tblView.backgroundView=imgView;

[self.view addSubview:tblView];

Upvotes: 1

Vishnu
Vishnu

Reputation: 354

Try This Code i am sure it'll work

UIImage * targetImage = [UIImage imageNamed:@"coloured_flower.jpg"];
UIGraphicsBeginImageContextWithOptions(tableView.frame.size, NO, 0.f);
[targetImage drawInRect:CGRectMake(0.f, 0.f, tableView.frame.size.width, tableView.frame.size.height)];
UIImage * resultImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
tableView.backgroundColor = [UIColor colorWithPatternImage:resultImage];

Upvotes: 1

Related Questions