简体   繁体   中英

ios - set gradient with custom colors

I am trying to set gradient on my main View with custom colors. View is displayed completely white though. What's wrong with this record?

 CAGradientLayer *gradient = [CAGradientLayer new];

 gradient.colors = @[(id)[UIColor colorWithRed:92.0 green:196.0 blue:244.0 alpha:1].CGColor,
                    (id)[UIColor colorWithRed:47.0 green:146.0 blue:229.0 alpha:1].CGColor];
gradient.frame = self.backGroundView.bounds;
gradient.locations = @[@0.5, @0.5];

[self.backGroundView.layer insertSublayer:gradient atIndex:0];

This is properly displayed with blue and red cut in half of view's height.

CAGradientLayer *gradient = [CAGradientLayer new];
gradient.colors = @[(id)[UIColor redColor].CGColor, (id)[UIColor blueColor].CGColor];
gradient.frame = self.backGroundView.bounds;
gradient.locations = @[@0.5, @0.5];

[self.backGroundView.layer insertSublayer:gradient atIndex:0];

your colors are wrong. the UIColor initializer expects values between 0 and 1. try this:

gradient.colors = @[(id)[UIColor colorWithRed:92/255.0 green:196/255.0 blue:244/255.0 alpha:1].CGColor,
                    (id)[UIColor colorWithRed:47/255.0 green:146/255.0 blue:229/255.0 alpha:1].CGColor];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM