Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting the alpha of a layer border?

I am setting a border to my layer by adding the following line of code:

[self.avatar.layer setBorderColor:[UIColor whiteColor].CGColor];

How can I change the alpha on just the border and not the avatar object as well?

like image 440
vzm Avatar asked Aug 20 '14 16:08

vzm


2 Answers

Use UIColor's +colorWithWhite:alpha:

[self.avatar.layer setBorderColor:[UIColor colorWithWhite:1.0f alpha:0.5f].CGColor];

If you need a color other than white, use +colorWithRed:green:blue:alpha: as already mentioned by user3386109.

like image 94
Guillaume Algis Avatar answered Nov 02 '22 22:11

Guillaume Algis


For Swift-3 Version

 let opacity:CGFloat = 0.6
 let borderColor = UIColor.white
 self.view.layer.borderColor = borderColor.withAlphaComponent(opacity).cgColor
like image 25
Shrawan Avatar answered Nov 02 '22 22:11

Shrawan