繁体   English   中英

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

[英]How to set UITableView background to image?

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

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

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

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

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

[YourView(或imageView)setbackgroundColor:[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];

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

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

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

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

GhostRider的替代解决方案无编码

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

暂无
暂无

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

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