pkc
pkc

Reputation: 8516

How to set Shadow on Bottom for UIView control + iOS swift

Before asking this question, I found so many answers at stack overflow but none worked for my case.

Requirement: enter image description here

I tried:

private func setShadow() {
    contentView.layer.masksToBounds = false
    contentView.layer.shadowOffset = CGSize(width: 0, height: 1.5)
    contentView.layer.shadowRadius = 0.5
    contentView.layer.shadowOpacity = 0.5
    layer.shadowColor = UIColor.lightGray.cgColor
}

I also play around with offset, radius, opacity; studied about each property, but none combination results in desired output. Any suggestions please.

Upvotes: 0

Views: 2113

Answers (1)

Elevo
Elevo

Reputation: 1041

You can achieve the effect by static layer.

contentView.layer.masksToBounds = false

let shadowLayer = CALayer()
shadowLayer.frame = contentView.bounds
shadowLayer.shadowOffset = CGSize(width: 0, height: 1.5);
shadowLayer.shadowRadius = 0.5
shadowLayer.shadowOpacity = 0.5;
shadowLayer.shadowColor = UIColor.lightGray.cgColor
contentView.layer.addSublayer(shadowLayer)

Please remember to input it in layoutSubviews if you use autolayout .

override func layoutSubviews() {
    super.layoutSubviews()
    shadowLayer.frame = contentView.bounds
}

Upvotes: 3

Related Questions