Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIView with blurred border

Is there a way to blur/fade out the border of a UIView?

i have done very little to none with core graphics so far.

like image 506
Padin215 Avatar asked Sep 10 '12 15:09

Padin215


2 Answers

You might try setting the UIView's CALayer with a large shadow radius and transparency. Something like:

    #include <QuartzCore/QuartzCore.h>

...

    CALayer *layer = myView.layer;
    layer.shadowOpacity = .5;
    layer.shadowColor = [[UIColor blackColor] CGColor];
    layer.shadowOffset = CGSizeMake(0,0);
    layer.shadowRadius = 10;
like image 103
Mike M Avatar answered Nov 15 '22 02:11

Mike M


Adding an example with the swift version:

    btnSignIn.layer.cornerRadius = btnSignIn.frame.height * 0.5
    btnSignIn.layer.shadowColor = UIColor.darkGray.cgColor
    btnSignIn.layer.shadowRadius = 4.0
    btnSignIn.layer.shadowOpacity = 1.0
    btnSignIn.layer.shadowOffset = CGSize(width: 0, height: 0)

Result:

enter image description here

Keep in mind that if you change the shadow settings in the ib editor, it will change the text's shadow, not the view's border.

Also you can make a class and set it in IB Builder:

class UIRoundedWhiteButton: UIButton {

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)

    layer.cornerRadius = frame.height * 0.5
    layer.shadowColor = UIColor.darkGray.cgColor
    layer.shadowRadius = 4.0
    layer.shadowOpacity = 1.0
    layer.shadowOffset = CGSize(width: 0, height: 0)
}...
like image 29
LightMan Avatar answered Nov 15 '22 03:11

LightMan