无法使用图层

时间:2017-03-03 10:33:42

标签: ios iphone uiviewcontroller calayer

我已经从stroyboard为uicontrollerview设置了颜色,但我想要背景颜色渐变。因此,在viewdidappear中编码所需的图层仍然没有出现。

-(void)viewDidAppear:(BOOL)animated{
    [self setBackGradient];
}

-(void)setBackGradient{
    CAGradientLayer *grad = [CAGradientLayer layer];
    grad.frame = self.view.bounds;

    UIColor *topColor=[UIColor colorWithRed:0.91 green:0.94 blue:0.99 alpha:1.0];
    UIColor *bottomColor=[UIColor colorWithRed:0.67 green:0.80 blue:0.93 alpha:1.0];

    NSArray *colorsArr=[[NSArray alloc]initWithObjects:topColor,bottomColor, nil];
    grad.colors=colorsArr;
    grad.masksToBounds=YES;
    [self.view.layer insertSublayer:grad atIndex:0];
}

1 个答案:

答案 0 :(得分:0)

图层的颜色需要为CGColor s。请尝试以下方法:

-(void)setBackGradient{

    CAGradientLayer *grad = [CAGradientLayer layer];
    grad.frame = self.view.bounds;

    NSArray *colorsArr=@[(id)[UIColor colorWithRed:0.91 green:0.94 blue:0.99 alpha:1.0].CGColor, (id)[UIColor colorWithRed:0.67 green:0.80 blue:0.93 alpha:1.0].CGColor];
    grad.colors=colorsArr;
    grad.masksToBounds=YES;
    [self.view.layer insertSublayer:grad atIndex:0];

}