Last Updated: December 26, 2018
·
12.67K
· e0d

Create view background gradient iOS, Objective C

Метод для создания градиента заднего фона вида:

+ (void)setBackgroundGradient:(UIView *)mainView color1Red:(float)colorR1 color1Green:(float)colorG1 color1Blue:(float)colorB1 color2Red:(float)colorR2 color2Green:(float)colorG2 color2Blue:(float)colorB2 alpha:(float)alpha
{

    [mainView setBackgroundColor:[UIColor clearColor]];
    CAGradientLayer *grad = [CAGradientLayer layer];
    grad.frame = mainView.bounds;

    grad.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithRed:colorR1/255.0 green:colorG1/255.0 blue:colorB1/255.0 alpha:alpha] CGColor], (id)[[UIColor colorWithRed:colorR2/255.0 green:colorG2/255.0 blue:colorB2/255.0 alpha:alpha] CGColor], nil];

    [mainView.layer insertSublayer:grad atIndex:0];   
}

Пример использования (example):

[self setBackgroundGradient:self.view color1Red:50.0 color1Green:63.0 color1Blue:86.0 color2Red:23.0 color2Green:26.0 color2Blue:29.0 alpha:1.0];