有没有办法在UITableViewController上设置背景视图?
我尝试使用我在UIViewController
上使用的代码,但视图会覆盖表视图的所有内容。如果我在cellForRowAtIndexPath-method
中添加背景视图,则根本不会显示。有没有人以前做过这个或者知道如何做到这一点?
这是我正在使用的代码:
UIImage *image = [UIImage imageNamed: @"background.jpg"];
UIImageView *backImage = [[UIImageView alloc] initWithImage: image];
[self.view addSubview: backImage];
[self.view sendSubviewToBack: backImage];
答案 0 :(得分:72)
我知道这已经很久了,但仅仅是为了记录......
我认为我使用UITableView.backgroundView
找到了更好的解决方案:
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"lisbon.png"]];
self.tableView.backgroundView = imageView;
[imageView release];
我尝试在iPhone上使用320x480的图像尺寸,并且效果很好(我也尝试过使用.jpg)。
答案 1 :(得分:68)
(这与Hans Espen上面的解决方案基本相同,但为了简洁起见使用方便的方法)
将其放入 - [UITableViewControllerSubclass viewDidLoad]
方法:
self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"BackgroundPattern.png"]];
在viewDidLoad方法中避免使用几个自动释放是没有意义的,因为它很少被调用(当视图实际加载时),因此对性能的影响可以忽略不计。
N.B。您应该始终在iPhone上使用PNG图像,而不是JPEG或任何其他格式。
答案 2 :(得分:6)
实际上,我开始工作了! :)
NSString *backgroundPath = [[NSBundle mainBundle] pathForResource:@"background" ofType:@"jpg"];
UIImage *backgroundImage = [UIImage imageWithContentsOfFile:backgroundPath];
UIColor *backgroundColor = [[UIColor alloc] initWithPatternImage:backgroundImage];
self.tableView.backgroundColor = backgroundColor;
[backgroundColor release];
答案 3 :(得分:5)
对于 Swift ,请使用此
self.tableView.backgroundView = UIImageView(image: UIImage(named: "backgroundImage.png"))
答案 4 :(得分:0)
self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"yourImage.png"]];
答案 5 :(得分:0)