Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableViewController Background Image

How can I set an image for UITableViewController?

I have used many things but it doesn't help me to set the image as background

UIImageView* bgView = [[[UIImageView alloc] initWithImage:
[UIImage imageNamed:@"Default.png"]] autorelease];

[tableView.backgroundView addSubview:bgView];

And this

[[self view] setAutoresizesSubviews:NO];

UIView *backgroundView = [[UIView alloc] initWithFrame:self.view.frame];
backgroundView.backgroundColor = 
  [UIColor colorWithPatternImage:
    [UIImage imageNamed:@"Default.png"]];

self.tableView.backgroundView = backgroundView;
self.tableView.backgroundColor = [UIColor clearColor];

[backgroundView release];

And

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

None of it worked for me.

like image 434
Youstanzr Avatar asked Apr 08 '12 20:04

Youstanzr


4 Answers

sets the background fill mode

[youTable setBackgroundView:
  [[UIImageView alloc] initWithImage:
    [UIImage imageNamed:@"bg.png"]]];

sets the background mode tile

[youTable setBackgroundColor:
  [UIColor colorWithPatternImage:
    [UIImage imageNamed:@"bg.png"]]];
like image 177
WhiteTiger Avatar answered Nov 18 '22 01:11

WhiteTiger


UIImage *image = [UIImage imageNamed:@"image.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

UITableView *tableView = (UITableView*)tableViewController.view;
tableView.backgroundView = imageView;
[imageView release];
like image 43
SVGreg Avatar answered Nov 18 '22 01:11

SVGreg


This worked for me :

tableView.backgroundView = 
  [[UIImageView alloc]initWithImage:
    [[UIImage imageNamed:@"background.png"] stretchableImageWithLeftCapWidth:0.0 
                                            topCapHeight:5.0]];
like image 3
Jeeva Karthik Avatar answered Nov 18 '22 01:11

Jeeva Karthik


If all the aforementioned suggestions don't work, just do it this way (the approach I always use):

  1. Create a UIViewController subclass.
  2. In viewDidLoad add both an UIImageView and an UITableView as subviews.
  3. Configure the tableView and implement the delegate methods. Set the background image in the imageView.

I think setting the backgroundView for a tableView can cause graphical errors (repeating images for each cell), so that's why I use the approach described here.

like image 1
Wolfgang Schreurs Avatar answered Nov 18 '22 01:11

Wolfgang Schreurs