简体   繁体   English

如何将UITableView背景设置为图像?

[英]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.) 我注意到许多应用程序都有UITableView的自定义背景图像(或者将背景设置为颜色。)

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) 我在UITableView类中找不到任何API来影响这一点,我在界面构建器中的尝试失败了(尝试设置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. 我自己也遇到了同样的问题,但我找到了TomSwift触及的方法。 As he mentioned, UITableView has a backgroundView property that is intended to do just what you're looking for. 正如他所提到的,UITableView有一个backgroundView属性,旨在满足您的需求。 If you want to set the background color, you should instead use the backgroundColor property, as others have said. 如果你想设置背景颜色,你应该使用backgroundColor属性,正如其他人所说的那样。 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]]; [YourView(或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. 设置tableView的backgroundView属性。 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. 您可以将其设置为已设置自定义backgroundColor的视图,也可以将其设置为已设置图像的UIImageView。

alternative solution of GhostRider No coding GhostRider的替代解决方案无编码

Make UiImageView by the interface builder, put your image in resources. 通过界面构建​​器创建UiImageView,将您的图像放入资源中。 set it in UIImageView Image property by the use of Inspector. 通过使用Inspector在UIImageView Image属性中设置它。 select table view and set it background color to clear(for this make opacity to 0 by using attribute inspector). 选择表视图并将其设置为要清除的背景颜色(对于此,使用属性检查器将不透明度设置为0)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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