如何在 swift 中为 UIView 提供阴影的同时去除某些 iPhone 中多余的黑色阴影?

How to remove extra black shadow in some iPhones while giving shadow for UIView in swift?

我给三视图加了阴影。阴影在 iPhone 8 plus、iPhone 11、iPhone 11 pro max 中完美呈现。如下所示

我在 iPhone 8、iPhone 7 plus、iPhone 7 的右侧出现深黑色阴影,为什么?如下所示

如何去除 iPhone8.

右侧的黑色

UIView 阴影代码如下:

firstContainerView.clipsToBounds = false
firstContainerView.layer.shadowColor = UIColor.black.cgColor
firstContainerView.layer.shadowOpacity = 1
firstContainerView.layer.shadowOffset = CGSize(width: 0.2, height: 0.2)
firstContainerView.layer.shadowRadius = 1
firstContainerView.layer.shadowPath = UIBezierPath(roundedRect: 
firstContainerView.bounds, cornerRadius: cornerRadius).cgPath

为什么有些设备会出现额外的黑影。 请帮我去除 iPhone 8, iPhone 7 plus.

阴影中多余的黑色

根据,您可以使用您的参数创建扩展:

extension UIView {
    func addShadow() {
        layer.shadowColor = UIColor.black.cgColor
        layer.shadowOffset = CGSize(width: 0.2, height: 0.2)
        layer.shadowRadius = 1
        layer.shadowOpacity = 1
        layer.masksToBounds = false

        updateShadow()
    }
    func updateShadow() {
        layer.shadowPath = UIBezierPath(roundedRect: self.bounds,cornerRadius: 5).cgPath
    }
}

并像这里一样在 viewDidLayoutSubviews() 中调用它

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    viewTest.addShadow()
}

我希望这对您有帮助 iPhone 7 等等