【发布时间】:2010-11-25 03:28:58
【问题描述】:
我见过一些 iPhone 应用程序使用自定义图像作为分组 UITableView 的背景,而不是标准的灰线。
这是如何实现的?
【问题讨论】:
标签: iphone image uitableview background
我见过一些 iPhone 应用程序使用自定义图像作为分组 UITableView 的背景,而不是标准的灰线。
这是如何实现的?
【问题讨论】:
标签: iphone image uitableview background
这就是对我有用的方法(一旦我弄清楚了就相当简单;)
1) 在您的应用委托中添加一个视图,并使其成为窗口的子视图:
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。
希望对您有所帮助, 骨干
【讨论】:
试试这个
- (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's 背景不透明度设置为 0%,然后使用 Interface Builder 在其后面简单地分层 UIImageView 来做到这一点。无论表格状态如何,这让我有一个固定的背景。您也可以将UITableView 的背景设置为图像,但随后背景会随表格滚动。 (目前我没有手头的代码,但不久前我在 Apple 开发者论坛上得到了提示)。
请注意,这可能会导致一些性能问题。 Apple 不鼓励尽可能使用透明度,因为 3GS 之前的型号上的 GPU 并不是特别强大。
【讨论】:
您可以像这样使用+[UIColor colorWithPatternImage:(UIImage)] 方法:
self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Background.png"]];
【讨论】:
colorWithPatternImage: 的问题是你需要使用“有图案”的图像,否则你的图像会被随机平铺
此链接有一个简单的解决方案,如果您希望所有视图具有相同的背景
【讨论】:
self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"SortByCategory_320x480.png"]];
self.tableView.separatorColor = [UIColor clearColor];
self.tableView.backgroundColor = [UIColor clearColor];
希望这会有所帮助。它不会在单元格后面显示可怕的半透明背景,尤其是在 Grouped UITableView 的情况下。
【讨论】: