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
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.
Objective-c version:
cell.backgroundColor = [UIColor clearColor];
Also see this: How to create a UITableViewCell with a transparent background
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
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
}
}
}
}
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];
UIImageView *imageviewTemp = [[UIImageView alloc] init];
[(UIImageView *)imageviewTemp sd_setImageWithURL:[NSURL URLWithString:self.stringOfPassedImage]];
[imageviewTemp setFrame:self.tableView.frame];
self.tableView.backgroundView = imageviewTemp;