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

3 iphone uitableview

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

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

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

rav*_*ron 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];
Run Code Online (Sandbox Code Playgroud)

要么:

// Set a solid color as the background of a UITableView called 'tableView'
// In this case I chose red
[tableView setBackgroundColor:[UIColor redColor]];
Run Code Online (Sandbox Code Playgroud)