What is the best way to create a shadow behind a UIImageView

IphoneUiimageviewShadow

Iphone Problem Overview


I have a UIImageView that I want to add a shadow behind. I wish that apple had that as a property but they have to make lots of things hard for us programmers so I need to ask this question.

Iphone Solutions


Solution 1 - Iphone

There's a better and easier way to do this. UIImageView inherits from UIView so it has a layer property. You can access the layer's shadow properties and bam, you got a shadow.

If you have the UIImageView as an IBOutlet to a nib file, you can just implement the awakeFromNib e.g.

Objective-C

- (void)awakeFromNib {
    imageView.layer.shadowColor = [UIColor purpleColor].CGColor;
    imageView.layer.shadowOffset = CGSizeMake(0, 1);
    imageView.layer.shadowOpacity = 1;
    imageView.layer.shadowRadius = 1.0;
    imageView.clipsToBounds = NO;
}

Don't forget to #import "QuartzCore/CALayer.h"


For Swift, you can go about it multiple ways. Create a class extension, subclass, or an imageView instance. Whichever the way, the process is the same in modifying the layers shadow property.

Swift 3

override func awakeFromNib() {
    super.awakeFromNib()
    
    imageView.layer.shadowColor = UIColor.purple.cgColor
    imageView.layer.shadowOffset = CGSize(width: 0, height: 1)
    imageView.layer.shadowOpacity = 1
    imageView.layer.shadowRadius = 1.0
    imageView.clipsToBounds = false
}

Solution 2 - Iphone

The simplest thing to do is add a shadow layer to your image view:

CALayer				*layer = [CALayer layer];
CGRect				bounds = self.bounds;

layer.bounds = bounds;
layer.position = CGPointMake(bounds.size.width / 2 + 3, bounds.size.height / 2 + 3);
layer.backgroundColor = [UIColor colorWithWhite: 0.25 alpha: 0.55].CGColor;
layer.zPosition = -5;

[self.layer addSublayer: layer];

Be sure "Clip Subviews" is turned off for the view

Solution 3 - Iphone

Swift solution with extension. Subclassing is not required. Call myImage.addShadow() from viewDidLoad(). This should work for UIView and UIImageView.

extension UIView {
    
    func addShadow() {
        layer.shadowColor = UIColor.black.cgColor
        layer.shadowOffset = CGSize(width: 0, height: 0)
        layer.shadowOpacity = 0.5
        layer.shadowRadius = 5
        clipsToBounds = false
    }
}

Solution 4 - Iphone

in additional to that, if you want to make white border and shadow you can use that code :

//shadow part
imageView.layer.shadowColor = [UIColor blackColor].CGColor;
imageView.layer.shadowOffset = CGSizeMake(0, 1);
imageView.layer.shadowOpacity = 1;
imageView.layer.shadowRadius = 1.0;
//white border part
[imageView.layer setBorderColor: [[UIColor whiteColor] CGColor]];
[imageView.layer setBorderWidth: 2.0];

Solution 5 - Iphone

Swift 5.x

profileImageView.layer.cornerRadius = profileImageView.frame.size.width/2
profileImageView.clipsToBounds = false
profileImageView.layer.shadowColor = UIColor.black.cgColor
profileImageView.layer.shadowOpacity = 0.7
profileImageView.layer.shadowOffset =  CGSize(width: 2, height: 2)
profileImageView.layer.shadowRadius = 10

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJabView Question on Stackoverflow
Solution 1 - IphoneAlex NguyenView Answer on Stackoverflow
Solution 2 - IphoneBen GottliebView Answer on Stackoverflow
Solution 3 - IphoneBoris Y.View Answer on Stackoverflow
Solution 4 - Iphonemturhan55View Answer on Stackoverflow
Solution 5 - IphoneAlessandro OrnanoView Answer on Stackoverflow