ios – 停止CALayer影子影响子视图?

我有一个定制的UIControl,我希望它有一个阴影,所以我设置相关属性在它的图层.在视图周围出现阴影,但阴影也会出现在UILabel的文本下,该文本是子视图.你如何阻止这个?我只想要外部超级视野的影子.
...
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
    ...
}

解决方法

当父视图的alpha值小于1.0或没有背景颜色(即设置为清除颜色)时,会发生这种情况.在这种情况下,阴影转换为子视图.有关详细信息,请参阅我的答案 here.

苹果Docs证明了这一点:

figure A-7 shows several different versions of the same sample layer with a red shadow applied. The left and middle versions include a background color so the shadow appears only around the border of the layer. However,the version on the right does not include a background color. In this case,the shadow is applied to the layer’s content,border,and sublayers.

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...