Before asking this question, I found so many answers at stack overflow but none worked for my case.
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.
CodePudding user response:
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()
// ...
}
CodePudding user response:
Change the shadow offset to make it towards to the bottom.
shadowOpacity has maximum value 1.0. (1.0 == dark shadow, 0.5 light shadow)
I tried
private func setShadow() {
viewShadow.layer.masksToBounds = false
viewShadow.layer.shadowOffset = CGSize(width: 0, height: 6)
viewShadow.layer.shadowRadius = 2.0
viewShadow.layer.shadowOpacity = 0.5
viewShadow.layer.shadowColor = UIColor.lightGray.cgColor
}
But the output is still not as expected.