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

Iphone 如何将UITableView背景设置为图像?,iphone,uitableview,Iphone,Uitableview,我注意到很多应用程序都有一个自定义的UITableView背景图像(或者将背景设置为颜色) 我在UITableView类中找不到任何影响此操作的API,并且我在interface builder中的尝试失败(尝试设置UIView的颜色) 有什么建议吗?我看到了一个带有图片的第三方应用程序,所以我知道它可以完成。设置tableView的backgroundView属性。您可以将其设置为已设置自定义背景颜色的视图,也可以将其设置为已设置图像的UIImageView。Ghostider的替代解决方案

我注意到很多应用程序都有一个自定义的UITableView背景图像(或者将背景设置为颜色)

我在UITableView类中找不到任何影响此操作的API,并且我在interface builder中的尝试失败(尝试设置UIView的颜色)


有什么建议吗?我看到了一个带有图片的第三方应用程序,所以我知道它可以完成。

设置tableView的backgroundView属性。您可以将其设置为已设置自定义背景颜色的视图,也可以将其设置为已设置图像的UIImageView。

Ghostider的替代解决方案
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];
无编码

通过界面生成器生成UiImageView, 把你的形象放在参考资料里。 使用Inspector在UIImageView图像属性中设置它。
选择“表格视图”,并将其背景色设置为“清除”(为此,请使用属性检查器将“不透明度”设置为0)。

在代码中要为图像设置背景色的位置添加此行

[YourView(或imageView)设置背景颜色:[UIColor] groupTableViewBackgroundColor]]


谢谢,试试这个。它对我有用

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

您需要添加一个图像视图,并将表格背景色设置为“清晰颜色”(参见l

)我自己也遇到了同样的问题,但我找到了TomSwift提到的一种方法。正如他所提到的,UITableView有一个backgroundView属性,它的目的就是做您想要做的事情。如其他人所说,如果要设置背景色,则应使用backgroundColor属性。因此:

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


我就是这样做的。可能不是最好的方法,但效果很好

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