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

时间:2013-05-25 15:39:36

标签: 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.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;

self.layer.hidden = YES;

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

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

6 个答案:

答案 0 :(得分:29)

我猜您可以使用shadowOpacity的{​​{1}}属性。

所以这应该有效:

CALayer

请参阅CALayer's shadowOpacity documentation page

并展示你的影子使用:

self.layer.shadowOpacity = 0.0;

答案 1 :(得分:6)

抱歉,不确定是否正确方式,但您是否尝试更改layer shadow的属性?例如,其中一个;

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

答案 2 :(得分:0)

你试图隐藏的“图层”是你有阴影的对象图层,它不是一个可见的方面..只有图层中的对象......无论如何都是相当混乱的概念化,删除阴影的唯一方法是撤消你最初做的事情,这是上面建议的,没有定义的属性,你只需要切换一个bool并使阴影消失

答案 3 :(得分:0)

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.00
    }
}

答案 4 :(得分: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,用作backgroundView cell.contentView

答案 5 :(得分:0)

我尝试了其他答案,唯一对我有用的是将[decimal]::MaxValue切换为([decimal]::MaxValue + 1).GetType().Name

layer.masksToBounds