简体   繁体   English

如何为分组表视图设置背景图像?

[英]How do I set a background image for a grouped table view?

I've seen some iPhone applications that use a custom image as the background for a grouped UITableView, instead of the standard gray lines. 我见过一些iPhone应用程序使用自定义图像作为分组UITableView的背景,而不是标准的灰色线条。

How is this achieved? 这是如何实现的?

Here's what worked for me (and fairly simple once I figured it out ;) 这对我有用(一旦我弄清楚就相当简单;)

1) Add a view in your app delegate and make it a subview of the window: 1)在app delegate中添加一个视图,并使其成为窗口的子视图:

UIView *bgView = [[UIView alloc]initWithFrame:window.frame];

 bgView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"screenBG.png"]];
 [window addSubview:bgView];
 [bgView release];

2) On each view controller .m file, under ViewDidLoad, set background color of that particular view to transparent (so the other bgView created above will show through): 2)在每个视图控制器.m文件上,在ViewDidLoad下,将该特定视图的背景颜色设置为透明(因此上面创建的其他bgView将显示):

self.view.backgroundColor = [UIColor clearColor];

And in my case, the view controller in step 2 was a tableviewcontroller. 在我的例子中,步骤2中的视图控制器是tableviewcontroller。 Looks great. 看起来很棒。

And BTW, doing the following in each view controller did NOT work well: 顺便说一句,在每个视图控制器中执行以下操作并不能很好地工作:

self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"screenBG.png"]];

So follow steps 1 and 2 above. 请按照上面的步骤1和2进行操作。

Hope this helps out, Tbone 希望这会有所帮助,Tbone

Try this 试试这个

- (void) viewDidLoad {
    [super viewDidLoad];

    self.tableView.backgroundColor = [UIColor clearColor];
    self.view.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed:@"wallpaper.png"]];
    self.tableView.opaque = NO;
}

In another project (developed using 2.2.1) I did this by setting my UITableView' s background opacity to 0%, and then simply layering a UIImageView behind it using Interface Builder. 在另一个项目(使用2.2.1开发)中,我通过将UITableView'的背景不透明度设置为0%,然后使用Interface Builder简单地在其后面层叠UIImageView来完成此操作。 This allowed me to have a fixed background regardless of the table state. 无论表状态如何,这都允许我具有固定的背景。 You can also set the background of the UITableView to be an image instead, but then the background scrolls with the table. 您也可以将UITableView的背景设置为图像,但随后背景会随表格滚动。 (I don't have the code handy at the moment, but I got the tip a while back on the Apple developer forums). (我目前没有方便的代码,但我在Apple开发者论坛上得到了一些提示)。

Note that this can cause some performance issues. 请注意,这可能会导致一些性能问题。 Apple discourages using transparency whenever possible because the GPUs on the pre-3GS models aren't particularly beefy. 苹果不鼓励尽可能使用透明度,因为3GS之前型号的GPU并不是特别强劲。

您可以像这样使用+[UIColor colorWithPatternImage:(UIImage)]方法:

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

the problem with colorWithPatternImage: is that you need to use "patterned" images, otherwise your image will be tiled randomly colorWithPatternImage的问题:你需要使用“图案化”图像,否则你的图像会随机平铺

this link has a simple solution, if you want all your views to have the same background 如果您希望所有视图具有相同的背景,则此链接有一个简单的解决方案

http://howtomakeiphoneapps.com/2009/03/how-to-add-a-nice-background-image-to-your-grouped-table-view/ http://howtomakeiphoneapps.com/2009/03/how-to-add-a-nice-background-image-to-your-grouped-table-view/

self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"SortByCategory_320x480.png"]];

self.tableView.separatorColor = [UIColor clearColor];

self.tableView.backgroundColor = [UIColor clearColor];

Hope this will help. 希望这会有所帮助。 It won't display hideous translucent background behind the cells especially in case of Grouped UITableView. 它不会在细胞后面显示可怕的半透明背景,特别是在Grouped UITableView的情况下。

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

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