Last Updated: September 11, 2018
· 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];

1 Response
Add your response


Pretty straight forward, the trick is to make the UIView a @IBDesignable, which will render it in Xocde. By making the UIColors @IBInspectable one can set them in Xcodes attribute inspector. https://ccleaner.vip/
Once you have that class in your project you can add Views to your storyboard and give them the class GradientView

3 months ago ·