iPhone - 在 UITableViewController 上设置背景 [英] iPhone - Setting background on UITableViewController

查看:18
本文介绍了iPhone - 在 UITableViewController 上设置背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在 UITableViewController 上设置背景视图?

Is there any way to set a background view on a UITableViewController?

我尝试使用我在 UIViewController 上使用的代码,但该视图覆盖了表视图的所有内容.如果我在 cellForRowAtIndexPath-method 中添加背景视图,它根本不显示.有没有人以前做过这件事,或者对如何做到这一点有想法?这是我正在使用的代码:

I try with the code I am using on a UIViewController, but the view comes over all the contents of the table view. If I add the background view in the cellForRowAtIndexPath-method, it is not showing at all. Has anyone done this before or have an idea on how it can be done? Here is the code I am using:

UIImage *image = [UIImage imageNamed: @"background.jpg"];
UIImageView *backImage = [[UIImageView alloc] initWithImage: image];
[self.view addSubview: backImage];
[self.view sendSubviewToBack: backImage];

推荐答案

(这与上面 Hans Espen 的解决方案基本相同,但为了简洁使用了方便的方法)

(This is basically the same as Hans Espen's solution above, but uses convenience methods for brevity)

把它放在你的 -[UITableViewControllerSubclass viewDidLoad] 方法中:

Put this in your -[UITableViewControllerSubclass viewDidLoad] method:

self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"BackgroundPattern.png"]];

在 viewDidLoad 方法中避免几次自动释放是没有意义的,因为它只会很少被调用(当视图实际加载时),因此对性能的影响可以忽略不计.

There's no point in avoiding a couple of autoreleases in a viewDidLoad method, since it only gets called rarely (when the view actually loads) and will therefore have negligible impact on performance.

注意您应该始终在 iPhone 上使用 PNG 图片,而不是 JPEG 或任何其他格式.

N.B. You should always use PNG images on the iPhone, not JPEG or any other format.

这篇关于iPhone - 在 UITableViewController 上设置背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆