2

我想给我的 UITableView 一个背景图片,所以我尝试了这个方法:

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
UIImageView *v = [[UIImageView alloc] initWithFrame:self.view.bounds];
[v setImage:[UIImage imageNamed:@"Background.png"]];
[self.view addSubview:v];
[self.view sendSubviewToBack: v];
...
}

这工作正常。但是,在 pushViewController 之后,这个方法就不再起作用了(为什么?)。所以我换了一个方法:

- (void)viewDidLoad
{
[super loadView];
// Do any additional setup after loading the view from its nib.
self.tableView.backgroundView = nil;
self.tableView.backgroundColor = [[UIColor alloc]initWithPatternImage:[UIImage imageNamed:@"Background.png"]];
...
}

这可以工作,但结果不是我所期望的。背景图片不知何故改变了它的大小(如部分放大),并且单元格的背景比前一个更暗。

有人知道原因吗?我被这个问题困扰了很长时间。

非常感谢!

4

3 回答 3

3

用这个:

UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"TableBackground.jpg"]];
self.tableView.backgroundColor = background;
[background release];

编辑:

UIImage *myImage = [[UIImage alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
myImage.image = [UIImage imageNamed:@"TableBackground.jpg"];
UIColor *background = [[UIColor alloc] initWithPatternImage:myImage];
self.tableView.backgroundColor = background;
[background release];
于 2013-07-30T08:36:46.067 回答
1

试试这个代码我相信它会工作

UIImage * targetImage = [UIImage imageNamed:@"coloured_flower.jpg"];
UIGraphicsBeginImageContextWithOptions(tableView.frame.size, NO, 0.f);
[targetImage drawInRect:CGRectMake(0.f, 0.f, tableView.frame.size.width, tableView.frame.size.height)];
UIImage * resultImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
tableView.backgroundColor = [UIColor colorWithPatternImage:resultImage];
于 2013-07-30T12:40:18.810 回答
1
UITableView *tblView=[[UITableView alloc] init];
[tblView setFrame:CGRectMake(0, 0, 320, 300)];


UIImageView *imgView=[[UIImageView alloc] initWithFrame:tblView.frame];
[imgView setImage:[UIImage imageNamed:@"cinemax.jpg"]];
tblView.backgroundView=imgView;

[self.view addSubview:tblView];
于 2013-07-30T13:00:59.023 回答