在swift4中为UIView添加阴影

时间:2017-11-22 15:29:44

标签: ios swift uiview

  

我想为我的UIView添加阴影(它不像默认情况下,它有点模糊和模糊),如下所示

enter image description here

我写了一个扩展名为

func addShadow(color: UIColor = UIColor.black, opacity: Float = 0.9, radius: CGFloat = 1, scale: Bool = true) {
        self.layer.masksToBounds = false
        self.layer.shadowColor = color.cgColor

        self.layer.shadowOpacity = opacity
        self.layer.shadowRadius = radius

        self.layer.shadowPath = UIBezierPath(rect: self.bounds).cgPath
        self.layer.shouldRasterize = true
        self.layer.rasterizationScale = scale ? UIScreen.main.scale : 1
    }

输出: enter image description here

但无法获得准确的输出。 感谢您的帮助。

3 个答案:

答案 0 :(得分:0)

我相信您还需要设置clipsToBounds

self.clipsToBounds = false

答案 1 :(得分:0)

我使用IBInspectable处理我的应用中的所有视图。试一试

extension UIView {

@IBInspectable var cornerRadius: CGFloat {
    set {
        layer.cornerRadius = newValue
    }
    get {
        return layer.cornerRadius
    }
}

@IBInspectable var borderWidth: CGFloat {
    set {
        layer.borderWidth = newValue
    }
    get {
        return layer.borderWidth
    }
}

@IBInspectable var borderColor: UIColor? {
    set {
        layer.borderColor = newValue?.cgColor
    }
    get {
        return UIColor(cgColor: layer.borderColor!)
    }
}

@IBInspectable var shadowOffset: CGSize {
    set {
        layer.shadowOffset = newValue
    }
    get {
        return layer.shadowOffset
    }
}

@IBInspectable var shadowOpacity: Float {
    set {
        layer.shadowOpacity = newValue
    }
    get {
        return layer.shadowOpacity
    }
}

@IBInspectable var shadowRadius: CGFloat {
    set {
        layer.shadowRadius = newValue
    }
    get {
        return layer.shadowRadius
    }
}

@IBInspectable var shadowColor: UIColor? {
    set {
        layer.shadowColor = newValue?.cgColor
    }
    get {
        return UIColor(cgColor: layer.shadowColor!)
    }
}

}

在你的情况下你应该修复shadowOffset属性。

答案 2 :(得分:0)

您应该在“containerView”中禁用 clipToBounds (将影子投放视图放置为子视图的视图)

看一个例子:

import UIKit

extension UIView {
    func addShadow(color: UIColor = UIColor.black, opacity: Float = 0.9, radius: CGFloat = 1, scale: Bool = true) {
        self.layer.masksToBounds = false
        self.layer.shadowColor = color.cgColor
        self.layer.shadowOpacity = opacity
        self.layer.shadowRadius = radius
        self.layer.shadowPath = UIBezierPath(rect: self.bounds).cgPath
        self.layer.shouldRasterize = true
        self.layer.rasterizationScale = scale ? UIScreen.main.scale : 1

    }
}

let shadowedView = UIView(frame: CGRect(x: 50, y: 50, width: 200, height: 100))
shadowedView.backgroundColor = .blue
shadowedView.layer.cornerRadius = 15.0
shadowedView.addShadow()

let mainView = UIView(frame: CGRect(x: 0, y: 0, width: 300, height:200))
mainView.backgroundColor = .white
mainView.addSubview(shadowedView)

enter image description here

这里我将添加一个视图容器并启用clipToBounds:

let shadowedView = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 100))
shadowedView.backgroundColor = .blue
shadowedView.layer.cornerRadius = 15.0
shadowedView.addShadow()

let containerView = UIView(frame: CGRect(x: 50, y: 50, width: 200, height: 100))
containerView.addSubview(shadowedView)
containerView.clipsToBounds = true

let mainView = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 200))
mainView.backgroundColor = .white
mainView.addSubview(containerView)

enter image description here