iOS从视图中添加/删除阴影

iOS从视图中添加/删除阴影,ios,uiview,Ios,Uiview,我不明白如何删除添加到视图中的阴影。 我在initWithFrame中向我的视图添加阴影,如下所示: self.layer.borderWidth = 2; self.layer.borderColor = [UIColor clearColor].CGColor; self.backgroundColor = [UIColor greenColor]; [self.layer setCornerRadius:8.0f]; CALayer *layer = self.layer; layer.s

我不明白如何删除添加到视图中的阴影。 我在
initWithFrame
中向我的视图添加阴影,如下所示:

self.layer.borderWidth = 2;
self.layer.borderColor = [UIColor clearColor].CGColor;
self.backgroundColor = [UIColor greenColor];
[self.layer setCornerRadius:8.0f];
CALayer *layer = self.layer;
layer.shadowOffset = CGSizeMake(2, 2);
layer.shadowColor = [[UIColor blackColor] CGColor];
layer.cornerRadius = 8.0f;
layer.shadowRadius = 3.0f;
layer.shadowOpacity = 0.80f;
layer.shadowPath = [[UIBezierPath bezierPathWithRect:layer.bounds] CGPath];
在应用程序执行之后,我想从这个视图中移除阴影。我试过使用:

layer.hidden = YES;

但这会完全隐藏视图,而不仅仅是添加的阴影

有没有办法从视图中检索添加的阴影,然后将其隐藏?
谢谢

我想您可以使用
CALayer
shadowOpacity
属性

因此,这应该是可行的:

self.layer.shadowOpacity = 0;

要显示您的阴影,请使用:

self.layer.shadowOpacity = 1.0;
抱歉,不确定正确的方法,但是您是否尝试过更改
层阴影的属性
?例如,其中之一

 layer.shadowOffset = CGSizeMake(0, 0);
 layer.shadowColor = [[UIColor clearColor] CGColor];
 layer.cornerRadius = 0.0f;
 layer.shadowRadius = 0.0f;
 layer.shadowOpacity = 0.00f;

您试图隐藏的“层”是对象的一个层,它有一个阴影,它不是一个可见的方面。。只有图层中具有的对象。。。不管怎么说,概念化是相当混乱的,移除阴影的唯一方法是撤销您最初所做的操作,正如上面所建议的,没有定义的属性,您可以只切换布尔并使阴影消失

Swift 4.2

我在标签和导航栏的代码中使用了它

extension UIView {

    func shadow(_ height: Int = 5) {
        self.layer.masksToBounds = false
        self.layer.shadowRadius = 4
        self.layer.shadowOpacity = 1
        self.layer.shadowColor = UIColor.gray.cgColor
        self.layer.shadowOffset = CGSize(width: 0 , height: height)
    }

    func removeShadow() {
        self.layer.shadowOffset = CGSize(width: 0 , height: 0)
        self.layer.shadowColor = UIColor.clear.cgColor
        self.layer.cornerRadius = 0.0
        self.layer.shadowRadius = 0.0
        self.layer.shadowOpacity = 0.0
    }
}

Swift 5.+

我的解决方案是添加一个
shadowBackgroundView
,它有一个可移动的
shadowLayer
。通过这种方式,我可以轻松地删除该层,而无需重置阴影属性

class ViewController: UIViewController {

    private let shadowBackgroundView: UIView = {
        let view = UIView(frame: CGRect(x: 100, y: 100, width: 100, height: 100))
        view.layer.masksToBounds = false
        return view
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.

        view.addSubview(shadowBackgroundView)

        // the view you want to add the shadow
        let dummyView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
        dummyView.backgroundColor = .red
        shadowBackgroundView.addSubview(dummyView)

        let addShadowButton = UIButton(frame: CGRect(x: 100, y: 300, width: 140, height: 50))
        addShadowButton.backgroundColor = .blue
        addShadowButton.setTitle("Add Shadow", for: .normal)
        addShadowButton.addTarget(self, action: #selector(addShadow), for: .touchUpInside)

        let removeShadowButton = UIButton(frame: CGRect(x: 100, y: 450, width: 140, height: 50))
        removeShadowButton.backgroundColor = .blue
        removeShadowButton.setTitle("Remove Shadow", for: .normal)
        removeShadowButton.addTarget(self, action: #selector(removeShadow), for: .touchUpInside)

        view.addSubview(addShadowButton)
        view.addSubview(removeShadowButton)
    }

    @objc
    func addShadow() {
        let shadowLayer = CALayer()
        shadowLayer.name = "ShadowLayer"
        shadowLayer.shadowColor = UIColor.black.cgColor
        shadowLayer.shadowOpacity = 1
        shadowLayer.shadowOffset = .zero
        shadowLayer.shadowRadius = 10
        shadowLayer.shadowPath = UIBezierPath(rect: shadowBackgroundView.bounds).cgPath
        // Otherwise the shadow will appear above the dummyView
        shadowLayer.zPosition = -1
        shadowBackgroundView.layer.addSublayer(shadowLayer)
    }

    @objc
    func removeShadow() {
        // Alternatively, you could also create the shadowLayer as a property, so you could call shadowLayer.removeFromSuperLayer()
        shadowBackgroundView.layer.sublayers?.first { $0.name == "ShadowLayer" }?.removeFromSuperlayer()
    }
}

需要注意的是,对于
UITableViewCell
,您不需要添加
shadowBackgroundView
,但是您可以将
shadowLayer
直接添加到
cell.view.layer
,它作为
cell.contentView

的背景视图。我尝试了其他答案,对我来说唯一有效的方法就是将
layer.masksToBounds
切换到
真/假

lazy var myView: UIView = {
    let view = UIView()
    view.translatesAutoresizingMaskIntoConstraints = false
    view.backgroundColor = .white
    view.layer.cornerRadius = 5
    view.layer.shadowColor = UIColor.black.cgColor
    view.layer.shadowOpacity = 3
    view.layer.shadowOffset = .zero
    view.layer.shadowRadius = 5
    return view
}()

func showShadow() {

    myView.layer.masksToBounds = false
}


func hideShadow() {

    myView.layer.masksToBounds = true
}

是否有一个理由在代码的中间声明一个本地的<代码>层/<代码>变量,指向“代码>自我?层< /代码>?我读到,这可能是更好的性能原因,用UIBezierPath声明一个阴影路径,不是吗?有一个最佳实践可以做到这一点吗?非常有用,我不考虑这个选项,但可能会影响性能更改此属性目标,然后尝试绝对删除?回答很好,唯一对我有效的方法。
lazy var myView: UIView = {
    let view = UIView()
    view.translatesAutoresizingMaskIntoConstraints = false
    view.backgroundColor = .white
    view.layer.cornerRadius = 5
    view.layer.shadowColor = UIColor.black.cgColor
    view.layer.shadowOpacity = 3
    view.layer.shadowOffset = .zero
    view.layer.shadowRadius = 5
    return view
}()

func showShadow() {

    myView.layer.masksToBounds = false
}


func hideShadow() {

    myView.layer.masksToBounds = true
}