I have a custom UIControl
, and I want it to have a shadow, so I set the relevant properties on its layer
. A shadow appears around the view as desired, but a shadow also appears under the text of a UILabel
, which is a subview. How do you stop this? I only want the shadow around the outer superview.
...
init() {
label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
self.translatesAutoresizingMaskIntoConstraints = false
addSubview(label)
self.layer.masksToBounds = false
self.layer.shadowColor = UIColor.blackColor().CGColor
self.layer.shadowOpacity = 1.0
self.layer.shadowRadius = 2.0
// Adding these lines trying to explicitly stop shadow on label...
label.layer.shadowOpacity = 0
label.layer.shadowColor = nil
...
}
Best Answer
This happens when parent view has alpha less than 1.0 or has no background color (i.e is set to clear color). In that case shadow translates to subviews. See my answer here for more details.
Apple Docs prove this: