iOS UITableView:使用CAGradientLayer将背景颜色指定为渐变 [英] iOS UITableView: assign background color as gradient using CAGradientLayer

查看:652
本文介绍了iOS UITableView:使用CAGradientLayer将背景颜色指定为渐变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始玩Xcode 4.3中的Master-Detail视图模板,我正在尝试更改master的背景颜色并将其设置为颜色渐变。
以下是我的尝试:

I just started playing with the Master-Detail view template in Xcode 4.3 and I am trying to change the background color of master and set it to a color gradient. Here is what I tried:

Colors.m

#import "Colors.h"

@implementation Colors

+ (UIColor *) navigationMenuGradientTop
{
    return [UIColor colorWithRed:213.0f/255.0f green:91.0f/255.0f blue:92.0f/255.0f alpha:1.0f];
}

+ (UIColor *) navigationMenuGradientBottom
{
    return [UIColor colorWithRed:188.0f/255.0f green:0.0f/255.0f blue:1.0f/255.0f alpha:1.0f];    
}

+ (CAGradientLayer *) navigationMenuGradient
{
    NSArray *colors = [NSArray arrayWithObjects:(id)self.navigationMenuGradientTop, self.navigationMenuGradientBottom, nil];
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.colors = colors;

    return gradientLayer;
}
@end

MasterViewController.m

MasterViewController.m

import "Colors.h"

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

    CAGradientLayer *bgLayer = [Colors navigationMenuGradient];
    bgLayer.frame = tableView.bounds;
    [tableView.layer insertSublayer:bgLayer atIndex:0];

    return cell;
}

运行后,我在 main <中出现以下错误/ code> - 主题1:EXC_BAD_ACCESS(代码= 1,地址= 0xxxxxxxx)

int main(int argc, char *argv[])
{
    @autorelasespool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}

我已将QuartzCore框架添加到项目中。我在这里错过了什么?当发生这样的错误时,我通常应该选择哪个方向(因为构建成功,app似乎已经崩溃了)?

I have added the QuartzCore framework to the project as well. What am I missing over here? and what direction should I generally go in when such errors occur (because the build succeeded, app seems to have crashed here)?

推荐答案

UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)] autorelease];
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = view.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor blackColor] CGColor], (id)[[UIColor whiteColor] CGColor], nil];
[view.layer insertSublayer:gradient atIndex:0];

来自: Gradients在UIView和UILabels在iPhone上

此代码可以帮助您解决问题。

this code can help your problem.

这篇关于iOS UITableView:使用CAGradientLayer将背景颜色指定为渐变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆