Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IOS: UIImageView set border white radius and shadow

Here is my code for set the border, shadow and corner

// set border
[self.avatarImageView.layer setBorderColor: [[UIColor whiteColor] CGColor]];
[self.avatarImageView.layer setBorderWidth: 2.0];

// set shadow
[self.avatarImageView.layer setShadowOffset:CGSizeZero];
[self.avatarImageView.layer setShadowOpacity:1.0];
self.avatarImageView.clipsToBounds = NO;

// set corner
self.avatarImageView.layer.cornerRadius = 10.0;
self.avatarImageView.layer.masksToBounds = YES;

If I only use the code for set border and set corner, it work well like this

enter image description here

But if I add the code set corner, I will got the result like this (the border and corner radius work, but the shadow is gone)

enter image description here

However the code for set corner work perfect if it stay alone. Please guide me what to do. Any help would be appreciate

Update
Follow @ozgur answer. Add 2 lines to my code, it will give a very beautiful view but the shadow is a little bit smaller

self.avatarImageView.layer.shouldRasterize = YES;
self.avatarImageView.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:self.avatarImageView.bounds cornerRadius:10].CGPath;

enter image description here

like image 524
Linh Avatar asked Mar 24 '16 03:03

Linh


2 Answers

Rounded corners requires masksToBounds to be set to YES. Because of that nothing past the boundaries (like a shadow) can be shown, because it will be masked/clipped off. If you disable masksToBounds so that it will show then the rounded corners wont work because they are unable to mask/clip your image into the rounded shape because you disabled masksToBounds.

Therefor, you can't do both at the same time on one view—so you need TWO views.

You need to make a UIView the same dimensions as your UIImageView and make the UIImageView a subview of your UIView.

Then on your UIImageView set masksToBounds to YES, and on its' superview (the UIView with the same dimensions) set masksToBounds to NO and add the properties accordingly.


Change your code to this: (typed it all w/o using xCode so it's possible I have typos)

UIView *avatarImageViewHolder = [[UIView alloc] initWithFrame:self.avatarImageView.frame];
avatarImageViewHolder.backgroundColor = [UIColor clearColor];
[avatarImageView.superview addSubview:avatarImageViewHolder];
avatarImageViewHolder.center = avatarImageView.center;
[avatarImageViewHolder addSubview:avatarImageView];
avatarImageView.center = CGPointMake(avatarImageViewHolder.frame.size.width/2.0f, avatarImageViewHolder.frame.size.height/2.0f);


self.avatarImageView.layer.masksToBounds = YES;
avatarImageViewHolder.layer.masksToBounds = NO;


// set avatar image corner
self.avatarImageView.layer.cornerRadius = 10.0;

// set avatar image border
[self.avatarImageView.layer setBorderColor: [[UIColor whiteColor] CGColor]];
[self.avatarImageView.layer setBorderWidth: 2.0];

// set holder shadow
[avatarImageViewHolder.layer setShadowOffset:CGSizeZero];
[avatarImageViewHolder.layer setShadowOpacity:1.0];
avatarImageViewHolder.clipsToBounds = NO;
like image 144
Albert Renshaw Avatar answered Nov 14 '22 23:11

Albert Renshaw


You need to add a container view and move your imageview inside that container view, Use this code after doing so:

CALayer *imageViewLayer= self.imageView.layer;
imageViewLayer.cornerRadius= 20.0f;
imageViewLayer.masksToBounds= YES;

CALayer *containerLayer = self.containerView.layer;
containerLayer.borderColor= [UIColor whiteColor].CGColor;
containerLayer.borderWidth= 3.0f;
containerLayer.cornerRadius= 20.0f;
containerLayer.shadowOffset = CGSizeMake(0, 0);
containerLayer.shadowColor = [UIColor blackColor].CGColor;
containerLayer.shadowRadius = 10.0f;
containerLayer.shadowOpacity = 0.80f;
containerLayer.masksToBounds= NO;
containerLayer.shadowPath = [[UIBezierPath bezierPathWithRect:containerLayer.bounds] CGPath];

Feel free to tweak the settings as per your need. Enjoy!

like image 39
atulkhatri Avatar answered Nov 14 '22 23:11

atulkhatri