Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set alpha of uiclearcolor?

I am currently doing this:

UIColor *myColor = [UIColor clearColor]; 

This is great but i would like to specify a certain alpha of "myColor". How would i do so?

like image 242
bobbypage Avatar asked Jul 02 '10 23:07

bobbypage


4 Answers

If you have an existing color, you can return a new one with a specified alpha, like this:

- (void)setBackgroundColor:(UIColor *)color  {     self.backgroundColor = [color colorWithAlphaComponent:0.3f]; } 
like image 181
James O'Brien Avatar answered Sep 23 '22 23:09

James O'Brien


[UIColor clearColor] is, how should I put it?, clear!

It is a convenience class method returning a UIColor with alpha at zero.

If you want a color with a fractional amount of transparency:

+ (UIColor *)colorWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha 

Or one of the other UIColor class methods.

like image 24
ohhorob Avatar answered Sep 26 '22 23:09

ohhorob


You can use colorWithWhite:alpha like so:

[UIColor colorWithWhite:1.0 alpha:0.5];

Or if you want a specific color:

[UIColor colorWithRed:1.0 green:1.0 blue:0.3 alpha:0.72];

Check out the docs.

like image 29
E.M. Avatar answered Sep 25 '22 23:09

E.M.


For Swift you can use:

UIColor.black.withAlphaComponent(0.5)

Or RGB Colors:

UIColor(red: 0/255.0 , green: 0/255.0 , blue: 0/255.0 , alpha: 0.5)
like image 30
Gerrit Post Avatar answered Sep 22 '22 23:09

Gerrit Post