简体   繁体   中英

How to set UITableView background to image?

I've noticed that a number of apps have a custom background image for UITableView (or have set the background to a color.)

I can't find any API in the UITableView class to affect this, and my attempts in interface builder have failed (trying to set the color of the UIView)

Any suggestions? I've seen a third party app with pictures, so I know it can be done.

I just ran into the same question myself, but I found a way to do it that TomSwift touched on. As he mentioned, UITableView has a backgroundView property that is intended to do just what you're looking for. If you want to set the background color, you should instead use the backgroundColor property, as others have said. So:

// Set an image as the background of a UITableView called 'tableView'
UIImageView *bg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"MyImage.png"]];
[tableView setBackgroundView:bg];

or:

// Set a solid color as the background of a UITableView called 'tableView'
// In this case I chose red
[tableView setBackgroundColor:[UIColor redColor]];
UIImageView *image = [[UIImageView alloc] initWithImage:
                                    [UIImage imagenamed:@"no_image.png"]];
[self.view addSubview:image];
UITableView *tableview = [[UITableView alloc] init];
[image addSubview:tableview];
tableview.backgroundcolor = [UIColor clearColor];

add this line in you code where you want to set background color for your image

[YourView(or imageView) setbackgroundColor:[UIColor groupTableViewBackgroundColor]];

Thanks

try this. it works for me.

CALayer *background = [CALayer layer];
background.zPosition = -1;
background.frame = self.view.frame;
background.contents = [[UIImage imageNamed:@"background.jpg"] CGImage];
[tableView.layer addSublayer:background];

您需要添加图像视图并将表格背景颜色设置为清除颜色,请参阅此链接以获取tutoria l

This is how I did it. Probably not the best way, but works well enough.

UIImage *bg = [UIImage imageNamed:@"bg"];
[bg drawInRect:self.view.window.frame];
UIImageView *bgView = [[UIImageView alloc] initWithFrame:self.view.window.frame];
bgView.image = bg;

activityTable.backgroundView = bgView;  //activityTable = UITableView

Set the backgroundView property of the tableView. You can set it to a view where you have set a custom backgroundColor, or you can set it to a UIImageView where you've set an image.

alternative solution of GhostRider No coding

Make UiImageView by the interface builder, put your image in resources. set it in UIImageView Image property by the use of Inspector. select table view and set it background color to clear(for this make opacity to 0 by using attribute inspector).

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