UITableView background Image

IphoneUitableviewBackgroundBackground Color

Iphone Problem Overview


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!

Iphone Solutions


Solution 1 - Iphone

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,

Solution 2 - Iphone

I think this approach cleaner:

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

Solution 3 - Iphone

Simple swift version will be

let tempImageView = UIImageView(image: UIImage(named: "yourImage"))
tempImageView.frame = self.tableView.frame
self.tableView.backgroundView = tempImageView;

Solution 4 - Iphone

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.

Solution 5 - Iphone

 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

Solution 6 - Iphone

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
            }
        }
    }

}

Solution 7 - Iphone

[TableView setBackgroundView:nil];
[TableView setBackgroundView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"apple.png"]] ];

Solution 8 - Iphone

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];

Solution 9 - Iphone

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.

Solution 10 - Iphone

Objective-c version:

cell.backgroundColor = [UIColor clearColor];

Also see this: https://stackoverflow.com/questions/1008246/how-to-create-a-uitableviewcell-with-a-transparent-background

Solution 11 - Iphone

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;

Solution 12 - Iphone

UIImageView *imageviewTemp = [[UIImageView alloc] init];
[(UIImageView *)imageviewTemp sd_setImageWithURL:[NSURL URLWithString:self.stringOfPassedImage]];
[imageviewTemp setFrame:self.tableView.frame];
self.tableView.backgroundView = imageviewTemp;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser730153View Question on Stackoverflow
Solution 1 - IphoneRavinView Answer on Stackoverflow
Solution 2 - IphoneNikolay KlimchukView Answer on Stackoverflow
Solution 3 - IphoneAvijit NagareView Answer on Stackoverflow
Solution 4 - IphoneNick ForgeView Answer on Stackoverflow
Solution 5 - IphoneJonaView Answer on Stackoverflow
Solution 6 - IphoneSigView Answer on Stackoverflow
Solution 7 - Iphoneanand madhavView Answer on Stackoverflow
Solution 8 - IphonematsrView Answer on Stackoverflow
Solution 9 - IphoneJFSView Answer on Stackoverflow
Solution 10 - IphoneLeon WangView Answer on Stackoverflow
Solution 11 - IphoneOz ShabatView Answer on Stackoverflow
Solution 12 - Iphonerajat kumarView Answer on Stackoverflow