donkey
donkey

Reputation: 4363

Xcode 7: Shadow does NOT show around UIView in Interface Builder

I want to see my UIView with added shadow like the example image below in storyboard. Unfortunately, there isn't an existing option to set it. Is there a way to set up the shadow using keypath so I can view the shadow in storyboard?

EDIT:

I have tried to render a shadow with the following code:

import Foundation
import UIKit

@IBDesignable

class ShadowedView: UIView {

    override func layoutSubviews() {
        super.layoutSubviews()

        let shadowPath = UIBezierPath(rect: self.bounds)
        self.layer.masksToBounds = false
        self.layer.shadowColor = UIColor.blackColor().CGColor
        self.layer.shadowOffset = CGSize(width: 0, height: 0.5)
        self.layer.shadowOpacity = 0.2
        self.layer.shadowPath = shadowPath.CGPath
        self.clipsToBounds = false

    }
}

@IBDesignable allowed me to see the changes immediately, but the shadow still is not present in the storyboard. Here is a screenshot of it:this

And the result should be the example image. Thank you!

Image credit to @Wezly

example of shadow

Upvotes: 3

Views: 858

Answers (1)

Losiowaty
Losiowaty

Reputation: 8006

You would need to override drawRect: or prepareForInterfaceBuilder depending on your needs. You can read more in the docs : https://developer.apple.com/library/ios/recipes/xcode_help-IB_objects_media/Chapters/CreatingaLiveViewofaCustomObject.html

Bottom line : you will have to add the code you posted in the drawRect: method in your custom view annotated as IBDesignable and Interface Builder will render it for you.

Upvotes: 1

Related Questions