UITableViewController Background Image
Asked Answered
L

9

9

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.

Latashialatch answered 8/4, 2012 at 20:49 Comment(0)
S
25

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"]]];
Septemberseptembrist answered 8/4, 2012 at 20:58 Comment(6)
I have test it in my project and it unfortunately doesn't workLatashialatch
my doubt is that you put a background color to cells, and these cover the background.Septemberseptembrist
my cells are default i didn't changed the color must i change it to clear color or the default is clear?Latashialatch
remember that its a UITableViewController no a tableviewLatashialatch
in viewDidLoad (for example) try to put: [self.tableView setBackgroundColor: [UIColor colorWithPatternImage: [UIImage imageNamed: @ "bg.png"]]]; then tableView: cellForRowAtIndexPath try putting cell.contentView.backgroundColor = [UIColor clearColor], so you put the background and make transparent the cell, but remember it depends on what you put in. You need to also make transparent the background of various objects, take for example the UILabel etc. ..Septemberseptembrist
it worked with me put there is a problem that the images only showing the top of its and repeat every cell i want the image display as one part all the imageLatashialatch
A
4
UIImage *image = [UIImage imageNamed:@"image.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

UITableView *tableView = (UITableView*)tableViewController.view;
tableView.backgroundView = imageView;
[imageView release];
Arriola answered 8/4, 2012 at 21:0 Comment(0)
R
3

This worked for me :

tableView.backgroundView = 
  [[UIImageView alloc]initWithImage:
    [[UIImage imageNamed:@"background.png"] stretchableImageWithLeftCapWidth:0.0 
                                            topCapHeight:5.0]];
Retrospect answered 12/9, 2013 at 6:8 Comment(1)
Good but explain it for better understanding of users.Gumma
C
1

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.

Carriole answered 8/4, 2012 at 21:5 Comment(0)
R
0

You can set the tableviews background view to a UIImageView instance (property backgroundView of UITableView).

Renayrenckens answered 8/4, 2012 at 21:0 Comment(0)
L
0

Following code is works well for me. Can you try following code:-

UIImageView *bgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Default.png"]];
bgView.frame = self.view.frame;
self.tableView.backgroundColor = [UIColor clearColor];
self.tableView.backgroundView = bgView;
[bgView release];
Loam answered 9/4, 2012 at 4:27 Comment(0)
T
0

i was also not able to set the backgroundColor of UITableView because it's a UITableViewController, my issue solved by this

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

As suggested by the "WhiteTiger".

Truda answered 15/1, 2013 at 7:52 Comment(2)
you're developing for the iPad? iPad setBackgroundColor does not work, you have to use necessarily setBackgroundViewSeptemberseptembrist
i am not using setBackgroundColor here , and i am working for iphoneTruda
R
0

This worked for me :

self.tableView.backgroundView = nil;
self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg2.png"]];

or

[self.tableView setBackgroundView:nil];
[self.tableView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"bg2.png"]]];
Roadstead answered 4/7, 2013 at 15:44 Comment(0)
H
0

Updating for Swift 4

The problem I was having was that I wanted to add a button and a background image to the UITableViewController. I added the button successfully, but every time I added the image container, it would replace the button instead of adding it below the button. So to get both, I added the following in the ViewWillAppear function:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    // Add a background view to the table view
    let backgroundImage = UIImage(named: "carbonfiber1.png")
    let imageView = UIImageView(image: backgroundImage)
    self.tableView.backgroundView = imageView
}
Hypercorrection answered 18/9, 2018 at 15:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.