渐变背景适用于iOS 8,但不适用于iOS 7

渐变背景适用于iOS 8,但不适用于iOS 7,ios,iphone,objective-c,ios7,ios8,Ios,Iphone,Objective C,Ios7,Ios8,因此,我正在构建一个以表视图为中心的应用程序,并尝试使用以下代码为每个表视图单元格添加渐变背景: CAGradientLayer *gradient = [CAGradientLayer layer]; gradient.frame = cell.bounds; gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithRed:0.94 green:0.84 blue:0.49 alpha:1.0]CGColor], (

因此,我正在构建一个以表视图为中心的应用程序,并尝试使用以下代码为每个表视图单元格添加渐变背景:

CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = cell.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithRed:0.94 green:0.84 blue:0.49 alpha:1.0]CGColor], (id)[[UIColor colorWithRed:0.93 green:0.86 blue:0.61 alpha:1.0]CGColor], nil];
[cell.layer insertSublayer:gradient atIndex:0];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.textColor = [UIColor colorWithRed:0.25 green:0.51 blue:0.00 alpha:1.0];
现在,在我使用的模拟器中,运行iOS 8(使用Xcode 6 Beta 2),我得到了以下结果:

但在我的设备上(对不起,辅助触摸,我的锁定按钮坏了…),运行iOS 7,它看起来像这样:

我在整个应用程序中的渐变在主视图和后台都起作用,只是表格单元格视图中的渐变不起作用。正如您在这些屏幕截图中所看到的,节标题视图也有渐变,但这两个版本都适用。是否有一个特定的标志,我需要寻找,使这在iOS 7的工作

编辑: 这是在CellForRowatineXpath中调用的

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = cell.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithRed:0.94 green:0.84 blue:0.49 alpha:1.0]CGColor], (id)[[UIColor colorWithRed:0.93 green:0.86 blue:0.61 alpha:1.0]CGColor], nil];
[cell.layer insertSublayer:gradient atIndex:0];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.textColor = [UIColor colorWithRed:0.25 green:0.51 blue:0.00 alpha:1.0];

NSDictionary *match;
if(indexPath.section == 0) {
    match = [_inprogressMatches objectAtIndex:indexPath.row];
} else if(indexPath.section == 1) {
    match = [_finalMatches objectAtIndex:indexPath.row];
} else {
    match = [_pregameMatches objectAtIndex:indexPath.row];
}

NSDictionary *homeTeam = [match objectForKey:@"homeTeamId"];
NSDictionary *awayTeam = [match objectForKey:@"awayTeamId"];    
cell.textLabel.text = [NSString stringWithFormat:@"%@ %@ : %@ %@",[awayTeam objectForKey:@"name"] , [match objectForKey:@"awayScore"], [match objectForKey:@"homeScore"], [homeTeam objectForKey:@"name"]];

return cell;
}

要在单元格背景中添加渐变,请不要弄乱其图层(在任何情况下,这都是错误的,因为您没有检查重用单元格中是否存在该图层)


给单元格一个
背景视图
,其层为渐变层或将渐变层作为子层。这就是背景视图的作用。

请显示更多代码。在哪里调用该代码?显示完整的上下文。您还可以显示不同的操作系统版本(7和8)。在iOS 7模拟器和iOS 8模拟器上看起来一样吗?为什么不设置手机的背景视图?真的没有必要像这样弄乱它的层,而且在任何情况下都可能是错误的(您可能会一次又一次地添加层,即使单元格已经有了层-尽管很难确定,因为您没有显示足够的代码).我同意@DavidRönnqvist-你为什么不使用iOS 7模拟器?所以在iOS 7模拟器中,同样的情况也会发生,我现在要再次检查,尽管这样做类似:[cell.backgroundView.layer insertSublayer:gradient atIndex:0];在iOS 8模拟器中似乎也不起作用。这就是你建议我做的吗?你必须做背景视图。我在这里讨论的实际代码:太棒了,谢谢matt!这很有魅力。上面的链接有我需要的一切