2009-10-21 47 views
4

我正在使用上传一张表的UITableViewController。我有一个UITableView在它的Nib文件。现在我想要从接口构建器或从TableViewController设置tableView的背景到图像。如何将UITableView的背景设置为图像?

如何做到这一点。

好吧,我在我的控制器中创建了一个UIImage。现在,当我添加时,我需要添加它。

当我尝试添加它并将tableView的颜色设置为clearColor时,它只是向我显示图像而不是表格,尽管我确保将图像发送到所有视图的背面。

伙计们请注意,我不是处理UIView并添加tableView作为其子视图但我正在处理UITableView。

回答

4

不知怎的,我在附近找到了一个方法。

self.tableView.backgroundColor = [UIColor clearColor]; 

self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithData:imageData]]; 
+0

这很有效,请注意,如果文件的图像不像视图那样大,则该图像将平铺,因此如果图像重复出现可以大大减少图像尺寸 – Amagrammer 2009-10-21 22:17:51

+0

访问parentViewController.view是一个可怕的想法 - 你不知道什么样的背部它有,这是一个非常糟糕的封装违规行为。即使它现在有效,下一次iOS更新可能会完全破坏您的代码。不要这样做。 – 2011-03-29 12:29:32

0

你可以做的是将tableview的backgroundColor属性设置为[UIColor clearColor],并在你的TableView下面有一个大小和位置相同的UIImageView来显示你的背景。

5

放置的UITableView后面一个UIImageView,然后做到这一点:

tableView.backgroundColor = [UIColor clearColor]; 
+0

还要确保。您使用的UITableViewCells(以及它们的子视图)具有透明背景 – 2009-10-21 18:05:42

0

的TableView属性包括背景颜色。将其设置为clearColor;并把它放在它后面。 (在IB我认为你必须删除桌面视图添加一个图像,然后再添加你的桌面视图)

如果你是UITableViewController的子类,你得到一个免费的桌面视图,不需要ivar或nib,但是因为你我正在使用背景,我会坚持使用IB。

1

方法rkbang大纲的backgroundColor = [UIColor colorWithPatternImage: image]伟大的作品。这是另一种通过向parentViewController添加新视图实现相同效果的方法。如果你想掩盖parentViewController可能具有的其他内容,这会很有用。

在你的UITableViewController子类,在-viewDidLoad插入:

//make a cool background image 
self.tableView.backgroundColor = [UIColor clearColor]; 
UIImage *patternImage = [UIImage imageNamed:@"backgroundImage.png"]; 
UIImageView * backgroundImageView = [[UIImageView alloc] initWithImage:patternImage]; 

[self.parentViewController.view addSubview:backgroundImageView]; 
[self.parentViewController.view sendSubviewToBack:backgroundImageView]; 
//release the background image and image view (the backgroundImageView is still retained by the parentViewController) 
[patternImage release]; 
[backgroundImageView release]; 

你不妨保持backgroundImageView的轨道,你可以删除或更换。上面的代码示例将其留给parentViewController来管理新视图。如果你正在加载和卸载这个UITableView,你会在每次加载时都泄漏这些UIImageViews,除非parentViewController以某种方式以相同的速率释放它们。

0

您可以使用表视图的backgroundView。这对我有效。

[self.tableView setBackgroundView: [[UIImageView alloc] initWithImage: [UIImage imageNamed:@"????.png"]]]; 

其中????。png是您的背景图片。

2

self.tableView.backgroundColor = [UIColor clearColor];

self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@“parentViewBackground。PNG“]];

图像尺寸应该是
2倍 - 640 * 960
1X - 320 * 480

大小比上面更小,它将被平铺

+0

我在我的'UITableView'后面添加了'UIImageView',并在'viewDidLoad'中设置了'backgroundColor'来清除。很棒!谢谢damithH – 2012-12-21 17:01:28

+0

哇....它工作正常..谢谢 – 2013-06-13 11:59:18

相关问题