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

时间:2010-12-01 04:11:44

标签: iphone uitableview

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

我在UITableView类中找不到任何API来影响这一点,我在界面构建器中的尝试失败了(试图设置UIView的颜色)

有什么建议吗?我见过带有图片的第三方应用,所以我知道可以做到。

8 个答案:

答案 0 :(得分:15)

我自己也遇到了同样的问题,但我找到了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];

或:

// Set a solid color as the background of a UITableView called 'tableView'
// In this case I chose red
[tableView setBackgroundColor:[UIColor redColor]];

答案 1 :(得分:2)

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

答案 2 :(得分:2)

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

  

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

由于

答案 3 :(得分:2)

试试这个。它对我有用。

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

答案 4 :(得分:1)

您需要添加图片视图并将表背景颜色设置为清除颜色,请参阅this link for tutoria l

答案 5 :(得分:0)

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

答案 6 :(得分:0)

GhostRider的替代解决方案 没有编码

通过界面构建​​器创建UiImageView, 把你的形象放在资源上。 通过使用Inspector在UIImageView Image属性中设置它。 选择表格视图并将其设置为要清除的背景颜色(对于此,使用属性检查器将不透明度设置为0)。

答案 7 :(得分:0)

这就是我做的。可能不是最好的方式,但效果还不错。

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
相关问题