1

I have placed a UIView extension in a utilities class written in swift.
However, when I try to call a method from the extension,

The compiler tells me the

The member does not exist

Here is my extension:

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
    }
}

Since UIImage subclasses UIView, I think I should be able to apply the method to a custom MKAnnotation image in a UIViewController by calling

myImage.addShadow()

However, I get error:

Value of type 'UIImage' has no member 'addShadow'

If I place the method in an extension of UIImage, I get same error.

Do I have to tell the UIViewController about the utilities class somehow or what could be wrong with my code?

user1904273
  • 4,562
  • 11
  • 45
  • 96

1 Answers1

2

You're making an extension for UIView. Not UIImageView

extension UIImageView {

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

Also please use the UIImageVIew directly, not the UIImage

EDIT

Assuming by your comments, i think you're trying to modify an MKAnnotationView. If so here's a question that will help you.

How to create rounded image with border and shadow as MKAnnotationView in Swift?

excitedmicrobe
  • 2,338
  • 1
  • 14
  • 30
  • It seems I can apply the method in the extenstion if it is to UIView to the annotationView itself. But the shadow is so subtle I can barely detect it. But that explains the error. Will mark correct when permitted. Thanks. – user1904273 Jan 28 '19 at 20:13
  • Well, if my answer was close to an explanation, I’d be grateful if you could give my the correct answer check mark, That helps me continue, thanks – excitedmicrobe Jan 28 '19 at 20:14