繁体   English   中英

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

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

我见过一些iPhone应用程序使用自定义图像作为分组UITableView的背景,而不是标准的灰色线条。

这是如何实现的?

这对我有用(一旦我弄清楚就相当简单;)

1)在app delegate中添加一个视图,并使其成为窗口的子视图:

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

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

2)在每个视图控制器.m文件上,在ViewDidLoad下,将该特定视图的背景颜色设置为透明(因此上面创建的其他bgView将显示):

self.view.backgroundColor = [UIColor clearColor];

在我的例子中,步骤2中的视图控制器是tableviewcontroller。 看起来很棒。

顺便说一句,在每个视图控制器中执行以下操作并不能很好地工作:

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

请按照上面的步骤1和2进行操作。

希望这会有所帮助,Tbone

试试这个

- (void) viewDidLoad {
    [super viewDidLoad];

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

在另一个项目(使用2.2.1开发)中,我通过将UITableView'的背景不透明度设置为0%,然后使用Interface Builder简单地在其后面层叠UIImageView来完成此操作。 无论表状态如何,这都允许我具有固定的背景。 您也可以将UITableView的背景设置为图像,但随后背景会随表格滚动。 (我目前没有方便的代码,但我在Apple开发者论坛上得到了一些提示)。

请注意,这可能会导致一些性能问题。 苹果不鼓励尽可能使用透明度,因为3GS之前型号的GPU并不是特别强劲。

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

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

colorWithPatternImage的问题:你需要使用“图案化”图像,否则你的图像会随机平铺

如果您希望所有视图具有相同的背景,则此链接有一个简单的解决方案

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];

希望这会有所帮助。 它不会在细胞后面显示可怕的半透明背景,特别是在Grouped UITableView的情况下。

暂无
暂无

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

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