I am trying to make a circle UIImageView
, and it works. Below is the way I use to make it:
[self.pic.layer setMasksToBounds:YES];
[self.pic.layer setCornerRadius:50.0];
I would like to add some shadow to the UIImageView
. The below code does add some shadow to my image view, however, the image view changes back to square shape. Can someone give me some pointers to solve this problem? Below is the code I use to add the shadow:
self.pic.layer.shadowColor = [UIColor purpleColor].CGColor;
self.pic.layer.shadowOffset = CGSizeMake(0, 1);
self.pic.layer.shadowOpacity = 1;
self.pic.layer.shadowRadius = 1.0;
self.pic.clipsToBounds = NO;
In case anyone looks for Swift 3 or 4 working solution:
Sometimes you also need to set constraints for image inside of the container, but it may work without it too in some cases. But if it's not, add this:
Use the
CALayer
'sshadowPath
property and add aUIBezierPath
with rounded rectEDIT
For a square-ish image view this technique does not work directly because, as you said, the image view goes back to square. Reason: You set
clipsToBounds = NO
to show the shadow which removes the clipping for corner radius, whereimageView
is subview ofcontainer
.Workaround:
Add your imageview in a container view and then apply the layer shadow to this container. Following is the code I tried.
The resultant effect is as shown in the screenshot,
Hope that helps!
Without a container but with a background view here is my 2 cents
As a swift 2.2 extension
I created custom classes (swift 3 or 4) and it works very well :
There are 2 classes to make a container and an image view round. And the main class which combines both of them: the one that you'll call.
I might be a bit late.
You can easily set the required attributes on the storyboard attribute inspector.
The result would be something like this.