UITableView background Image
Asked Answered
S

12

64

I'am trying to setup a png image as my tableview's background. With the following code all are fine! But only on iPhone Simulator. If I try to run the application on an iPhone device the background of tableview remains white (or clear). Do you thing thing that is something about the way I tried to set the background color. I have been trying many ways until the following, but all have the same issue.

self.tableView.backgroundColor = [UIColor clearColor];
self.tableView.opaque = NO;
self.tableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"TableViewBackground.png"]];

Thank you in advance!

Samoyedic answered 28/4, 2011 at 21:42 Comment(4)
This sounds like the image is not copied to your device. What does [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"TableViewBackground.png"]]; return on the device? Dump it to the console, please.Calliope
I think Nick is right, you can dump it using debugger while breakpointing by typing : po [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"TableViewBackground.png"]] ;) clean/rebuild might help.Kennie
Or just NSLog(@"imageView: %@", [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"TableViewBackground.png"]]);Calliope
This is the NSLog output : 2011-04-29 01:13:42.653 TestProject[573:707] imageView: <UIImageView: 0x14aac0; frame = (0 0; 0 0); userInteractionEnabled = NO; layer = <CALayer: 0x151a40>> and this is from simulator 2011-04-29 01:15:21.431 TestProject[3893:207] imageView: <UIImageView: 0x5923cc0; frame = (0 0; 320 480); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x4e3ec00>> It seems to have issue with image (frame = 0,0,0,0???) I use this image also for launch image!Samoyedic
B
174

Please use following code.

UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"TableViewBackground.png"]];
[tempImageView setFrame:self.tableView.frame]; 

self.tableView.backgroundView = tempImageView;
[tempImageView release];

Thanks,

Baseless answered 29/4, 2011 at 4:12 Comment(5)
This had been annoying me for ages. Thanks so much! :)Otalgia
this is better than change backgroundColor as it won't repeat the background when the table is scrolled or bouncing.Beltran
Setting the imageview to the frame is actually bad, since you are taking the origin of the table view, which could potentially be nonzero, while the image view should probably be zero origin alwaysPablopabon
Use bounds instead [tempImageView setFrame:self.tableView.bounds];Imprisonment
i see my background for a moment, but then background is replaced by cell's content. What should i do ?Chuffy
S
17

I think this approach cleaner:

UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"TableBackground.jpg"]];
self.tableView.backgroundColor = background;
[background release];
Strontian answered 3/1, 2012 at 2:56 Comment(3)
do you know how to make it fixed in its position so that it wont scroll with the table? and also make the image start "X" pixels from the top?Kirkendall
this would cause performance issues on uitableview and scrollviewBentlee
Using this solution on retina devices causes images to stretch. Just something to watch out for.Ensepulcher
D
14

Simple swift version will be

let tempImageView = UIImageView(image: UIImage(named: "yourImage"))
tempImageView.frame = self.tableView.frame
self.tableView.backgroundView = tempImageView;
Disputable answered 28/2, 2016 at 13:39 Comment(1)
Is this working on static tableviews or only working on dynamic cells ? As I've tries to use it on static but it doesn't workPistoleer
R
7

Is your image actually named TableViewBackground.PNG (note the capitals)? You need to have the case match exactly on an iOS device, whereas it doesn't need to match exactly in the Simulator.

Rabah answered 29/4, 2011 at 4:22 Comment(0)
C
5
 UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"TableViewBackground.png"]];
[tempImageView setFrame:self.tableView.frame]; 

self.tableView.backgroundView = tempImageView;
 [tempImageView release];

This works perfect. Thanks to Ravin by Jona

Churchy answered 26/3, 2013 at 11:38 Comment(0)
C
4

An IBDesignable tableview subclass that will let you attach an image from interface builder

import UIKit

@IBDesignable
class DesignableTableView: UITableView {

    @IBInspectable var backgroundImage: UIImage? {
        didSet {
            if let image = backgroundImage {
                let backgroundImage = UIImageView(image: image)
                backgroundImage.contentMode = UIViewContentMode.ScaleToFill 
                backgroundImage.clipsToBounds = false
                self.backgroundView = backgroundImage
            }
        }
    }

}
Cowey answered 25/8, 2015 at 23:5 Comment(0)
L
2
[TableView setBackgroundView:nil];
[TableView setBackgroundView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"apple.png"]] ];
Longe answered 12/8, 2014 at 12:59 Comment(0)
T
1

This works perfectly fine, also if you want to use a pattern image for your background.

UIImage *image = [UIImage imageNamed:@"something.png"];
self.tableView.backgroundView = nil;
self.view.backgroundColor = [UIColor colorWithPatternImage:image];
Thermolabile answered 22/6, 2013 at 23:50 Comment(0)
G
0

A little late but maybe for all the others looking for a solution. I could get that work by using the following code in the viewDidLoad method of the UITableViewController:

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

or with using just a background color:

self.parentViewController.view.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
self.tableView.backgroundColor = [UIColor clearColor];

It took me a while to figure that out but now it works like a charm.

Grimmett answered 12/8, 2013 at 17:50 Comment(0)
N
0

Objective-c version:

cell.backgroundColor = [UIColor clearColor];

Also see this: How to create a UITableViewCell with a transparent background

Nauseating answered 21/12, 2016 at 9:32 Comment(0)
U
0

Thanks @Ravin for your answer.

In Swift 5.0:

    let tempImageView = UIImageView(image: UIImage(named: "justin_bieber_topless.png"))
    tempImageView.frame = self.tableView.frame
    self.tableView.backgroundView = tempImageView;
Upheaval answered 20/5, 2020 at 14:24 Comment(0)
S
-3
UIImageView *imageviewTemp = [[UIImageView alloc] init];
[(UIImageView *)imageviewTemp sd_setImageWithURL:[NSURL URLWithString:self.stringOfPassedImage]];
[imageviewTemp setFrame:self.tableView.frame];
self.tableView.backgroundView = imageviewTemp;
Slaton answered 4/8, 2016 at 7:23 Comment(2)
where are you setting the background image on the table view? you are not answering the questionIngrain
Nothing in the original question implies that the image must be fetched from network. furthermore sd_setImageWithURL: isn't available through UIKit.Polytonality

© 2022 - 2024 — McMap. All rights reserved.