Rob
Rob

Reputation: 4224

iOS : how to shrink a UIView.layer.shadow to a desired width?

I have been adding a bottom shadow to a UIButton this way:

class MyButton: UIButton {
    override func layoutSubviews() {
        super.layoutSubviews()
        self.layer.shadowOpacity = 0.33
        self.layer.shadowRadius = 4.0
        self.layer.shadowColor = UIColor.blue.cgColor
        self.layer.shadowOffset = CGSize(width: 0.0, height: 6.0)
    }
}

The result I'm getting:

enter image description here

This works fine but I would like now to shrink the shadow width in order to have this rendering instead (picture from Sketch) :

enter image description here

Any idea of how to handle this?

Thanks for your help!

Upvotes: 0

Views: 67

Answers (2)

tailor
tailor

Reputation: 665

Use layer's shadowPath property. add below code to layoutSubviews method and try.

let path = UIBezierPath(roundedRect: bounds.insetBy(dx: 10, dy: 0), cornerRadius: 4.0)
self.layer.shadowPath = path.cgPath

Upvotes: 1

maxwell
maxwell

Reputation: 4156

If you do not find a more suitable answer, then try this: Add a UIView under the button with the desired size and it is for her to make a shadow.

Upvotes: 0

Related Questions