简体   繁体   中英

How to set the background of a UITableView to an image?

I am using a UITableViewController which uploads a table. I have a Nib File with UITableView in it.Now I want to set the background of the tableView either from interface builder or from the TableViewController to an image.

How to do that.

OK so I created an UIImage in my controller. Now when I add where do I need to add it.

When I try adding it and set the color of tableView to clearColor, it just shows me the Image and not the table although I make sure that image is being sent to back of all views.

Guys Please note that I am not dealing a UIView and adding a tableView as its subview But I am dealing with a UITableView .

将UIImageView放在UITableView后面,然后执行以下操作:

tableView.backgroundColor = [UIColor clearColor];

Somehow playing around I was able to find out a way.

self.tableView.backgroundColor = [UIColor clearColor];

self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithData:imageData]];

self.tableView.backgroundColor = [UIColor clearColor];

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

the image size should be
2x - 640*960
1x - 320*480

the size smaller than above, it will be tiled.

The backgroundColor = [UIColor colorWithPatternImage: image] method rkbang outlines works great. Here is another method to achieve the same effect by adding a new view to the parentViewController. This would be useful if you want to mask other contents the parentViewController might have.

In your UITableViewController subclass, in -viewDidLoad insert:

//make a cool background image
self.tableView.backgroundColor = [UIColor clearColor];
UIImage *patternImage = [UIImage imageNamed:@"backgroundImage.png"];
UIImageView * backgroundImageView = [[UIImageView alloc] initWithImage:patternImage];

[self.parentViewController.view addSubview:backgroundImageView];
[self.parentViewController.view sendSubviewToBack:backgroundImageView];
//release the background image and image view (the backgroundImageView is still retained by the parentViewController)
[patternImage release];
[backgroundImageView release];

You may wish to keep track of the backgroundImageView so you can remove it or replace it. The code example above leaves it to the parentViewController to manage the new view. If you're loading and unloading this UITableView, you'll be leaking these UIImageViews with every load unless the parentViewController is releasing them at the same rate somehow.

你可以做的是将tableview的backgroundColor属性设置为[UIColor clearColor],并在TableView下面有一个相同大小和位置的UIImageView来显示你的背景。

TableView properties include background color. Set this to clearColor; and put the image behind it. (In IB I think you'll have to delete the tableview add an image and then add your tableview back again)

if you're subclassing UITableViewController you get a tableview for free, no ivar or nib required, however since you're using a background I would stick with IB.

You can use the table-view's backgroundView. This worked for me.

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

where ????.png is your background image.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM