UIView 视图渐变色填充

//

//  ViewController.m

//  cocoa

//

//  Created by tlcsky on 2021/8/22.

//


#import "ViewController.h"


@interface ViewController ()


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    CGRect rect = CGRectMake(10, 60, 200, 200);

    UIView *gradientView= [[UIView alloc] initWithFrame:rect];

    

    CAGradientLayer *gradientLayer = [CAGradientLayer layer];

    gradientLayer.frame = gradientView.frame;

    

    CGColorRef fromColor = [UIColor yellowColor].CGColor;

    CGColorRef midColor = [UIColor redColor].CGColor;

    CGColorRef toColor = [UIColor purpleColor].CGColor;

    

    gradientLayer.colors =[NSArray arrayWithObjects:(__bridge id)(fromColor),midColor,toColor,nil];

    [gradientView.layer addSublayer:gradientLayer];

    [self.view addSubview:gradientView];

}

    

@end