iOS UIView背景色渐变


     UIView *theView = [[UIView alloc] initWithFrame:self.headerBackImgView.frame];

    [self.headerView addSubview:theView];

    

    //初始化CAGradientlayer对象,使它的大小为UIView的大小

    CAGradientLayer *gradientLayer = [CAGradientLayer layer];

    gradientLayer.frame = theView.bounds;

    

    //CAGradientlayer对象添加在我们要设置背景色的视图的layer

    [theView.layer addSublayer:gradientLayer];

    

    //设置渐变区域的起始和终止位置(范围为0-1

    gradientLayer.startPoint = CGPointMake(0, 0);

    gradientLayer.endPoint = CGPointMake(1, 1);

    

    //设置颜色数组

    gradientLayer.colors = @[(__bridge id)[UIColor colorFromHexCode:@"0488e5"].CGColor,

                                  (__bridge id)[UIColor colorFromHexCode:@"#20bcf4"].CGColor];

    

    //设置颜色分割点(范围:0-1

    gradientLayer.locations = @[@(0.0f), @(1.0f)];

你可能感兴趣的:(iOS-问题汇总)