iOS UITableView:使用 CAGradientLayer 将背景颜色指定为渐变

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10225132/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:57:22  来源:igfitidea点击:

iOS UITableView: assign background color as gradient using CAGradientLayer

iosuitableviewxcode4.3cagradientlayer

提问by vikmalhotra

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:

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

Colors.m

颜色.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

主视图控制器.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;
}

Upon running I get the following error in main- Thread 1: EXC_BAD_ACCESS (code=1, address=0xxxxxxxx)

运行后,我收到以下错误main-Thread 1: EXC_BAD_ACCESS (code=1, address=0xxxxxxxx)

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

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)?

我也在项目中添加了 QuartzCore 框架。我在这里错过了什么?发生此类错误时,我通常应该往哪个方向走(因为构建成功,应用程序似乎在这里崩溃了)?

回答by Ankit Gupta

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];

from: Gradients on UIView and UILabels On iPhone

来自:iPhone 上 UIView 和 UILabels 上的渐变

this code can help your problem.

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

回答by Jaminyah

In order to add a background gradient to a view I followed the tutorial at this link:

为了向视图添加背景渐变,我按照此链接中的教程进行操作:

http://danielbeard.wordpress.com/2012/02/25/gradient-background-for-uiview-in-ios/

http://danielbeard.wordpress.com/2012/02/25/gradient-background-for-uiview-in-ios/

In my case I had a UITableView and wanted the gradient to be in the background. When I used the code below from the link my tableview disappeared.

就我而言,我有一个 UITableView 并希望渐变位于背景中。当我使用以下链接中的代码时,我的 tableview 消失了。

-(void) ViewWillAppear:(BOOL) animated {   
CAGradientLayer *bgLayer = [BackgroundLayer blueGradient];
bgLayer.frame = self.view.bounds;
[self.view.layer insertSublayer:bgLayer atIndex:0];
}

On modifying my code as below, it was possible to have a background gradient and a tableview at the same time.

在如下修改我的代码时,可以同时拥有背景渐变和表格视图。

-(void) ViewDidAppear:(BOOL) animated {   
CAGradientLayer *bgLayer = [BackgroundLayer blueGradient];
bgLayer.frame = self.view.bounds;
[self.tableView.layer insertSublayer:bgLayer atIndex:0];
}

回答by Nishant

hey check out this guy's blog here. This helped me quite a lot! I modified the class as per my requirements and VOILA!

嘿,在这里查看这个人的博客。这对我帮助很大!我根据我的要求修改了课程,瞧!

http://danielbeard.wordpress.com/2012/02/25/gradient-background-for-uiview-in-ios/

http://danielbeard.wordpress.com/2012/02/25/gradient-background-for-uiview-in-ios/