如何在iOS中的UITableViewCell中制作渐变背景?

时间:2011-08-04 15:46:28

标签: iphone ipad

我按照下面的教程使用CAGradientLayer在UITableViewCell中制作渐变背景。

http://cocoawithlove.com/2009/08/adding-shadow-effects-to-uitableview.html

除了本教程,本主题还有其他任何资源吗?

感谢。

4 个答案:

答案 0 :(得分:39)

总是很棒的Ray Wenderlich做了一个关于更改UITableViewCells的教程并包含了一个渐变。

http://www.raywenderlich.com/2033/core-graphics-101-lines-rectangles-and-gradients

如果你想快速和方式,这里有一些代码:

//include #import <QuartzCore/QuartzCore.h> in the header…

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

 if (cell == nil) {
        cell = [[DayCalendarCellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        CAGradientLayer *gradient = [CAGradientLayer layer];
        gradient.frame = cell.bounds;
        gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor whiteColor]CGColor], (id)[[UIColor redColor]CGColor], nil];
        [cell.layer addSublayer:gradient];
    }        
    return cell;
}

你可以改变颜色,但这会给你一个好主意......

祝你好运!

答案 1 :(得分:2)

创建X高度的1px图像。

在cellForRowAtIndexPath方法的单元格中添加UIView对象。

在UIView setbackgroundcolor方法中使用uicolor的colorWithPatterImage方法。

答案 2 :(得分:2)

按照雅各布的回答并设置UIButton或任何View使用的背景颜色:

[button.layer insertSublayer:gradient atIndex:0];

而不是:

[button.layer addSublayer:gradient];

答案 3 :(得分:1)

Peronally我会在Photoshop或gimp中创建渐变图像,然后将其用作背景。只要细胞具有可预测的大小,就应该可以正常工作。