如何在UIView顶部设置阴影?

时间:2019-05-14 04:45:46

标签: ios swift

class AccountListViewController: UIViewController {       

    @IBOutlet weak var actionButtonView: UIView!

    override func viewDidLoad() {            
       super.viewDidLoad()
       actionButtonView.layer.cornerRadius = 10
       actionButtonView.layer.shadowColor = UIColor.gray.cgColor
       actionButtonView.layer.shadowOffset = .zero
       actionButtonView.layer.shadowOpacity = 0.6
       actionButtonView.layer.shadowRadius = 10
    }

我已经尝试过了,但是没有得到结果

5 个答案:

答案 0 :(得分:0)

您的代码很好,但是您需要为阴影偏移量指定负值而不是.zero

class AccountListViewController: UIViewController {

    @IBOutlet weak var actionButtonView: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()
        actionButtonView.layer.cornerRadius = 10
        actionButtonView.layer.shadowColor = UIColor.gray.cgColor
        actionButtonView.layer.shadowOffset = CGSize(width: 0.0, height : -5.0)
        actionButtonView.layer.shadowOpacity = 0.6
        actionButtonView.layer.shadowRadius = 10
    }
}

为方便起见,您可以为此创建扩展名。

请参考以下代码

extension UIView {
    func addTopShadow(shadowColor : UIColor, shadowOpacity : Float,shadowRadius : Float,offset:CGSize){
        self.layer.shadowColor = shadowColor.cgColor
        selflayer.shadowOffset = offset
        self.layer.shadowOpacity = shadowOpacity
        self.layer.shadowRadius = shadowRadius
        self.clipsToBounds = false
    }
}

如何使用?

viewName.addTopShadow(shadowColor: UIColor.gray, shadowOpacity: 0.9, shadowRadius: 10, offset: CGSize(width: 0.0, height : -5.0))

答案 1 :(得分:0)

尝试一下:-

override func viewDidLoad() {

        super.viewDidLoad()

        actionButtonView.layer.masksToBounds = false
        actionButtonView.layer.cornerRadius = 10
        seactionButtonViewlf.layer.shadowColor = UIColor.gray.cgColor
        actionButtonView.layer.shadowPath = UIBezierPath(rect: CGRect(x: -5,y: -5, width: 5, height: 5)).cgPath 
        actionButtonView.layer.shadowOffset = CGSize(width: 0.0, height: 0.0)
        actionButtonView.layer.shadowOpacity = 0.6 
        actionButtonView.layer.shadowRadius = self.layer.frame.height / 5 //Shadow Radius you want

    }

答案 2 :(得分:0)

以下功能为我工作:

func addTopShadow(forView view: UIView, shadowHeight height: CGFloat = 5) {
            let shadowPath = UIBezierPath()
            shadowPath.move(to: CGPoint(x: 0, y: 0))
            shadowPath.addLine(to: CGPoint(x: view.bounds.width, y:0))
            shadowPath.addLine(to: CGPoint(x: view.bounds.width-20, y: view.bounds.height ))
            shadowPath.addLine(to: CGPoint(x: view.bounds.width-20, y: view.bounds.height))
            shadowPath.close()

            view.layer.shadowColor = UIColor.red.cgColor
            view.layer.shadowOpacity = 0.5
            view.layer.masksToBounds = false
            view.layer.shadowPath = shadowPath.cgPath
            view.layer.shadowRadius = 2
        }

使用方法:

self.addTopShadow(forView: self.customView, shadowHeight: 1)

答案 3 :(得分:0)

在您的代码中,只需将shadowOffset更改为以下值,而不是将其设置为zero

actionButtonView.layer.shadowOffset = CGSize(width: 0, height: -3)

还要减小shadowRadius的值,以便可以看到效果。

actionButtonView.layer.shadowRadius = 3

enter image description here

答案 4 :(得分:0)

您可以按照以下方式进行操作:

extension UIView {

func addshadow(top: Bool,left: Bool,bottom: Bool,right: Bool,shadowRadius: CGFloat = 2.0) {

    self.layer.masksToBounds = false
    self.layer.shadowOffset = CGSize(width: 0.0, height: 0.0)
    self.layer.shadowRadius = shadowRadius
    self.layer.shadowOpacity = 1.0

    let path = UIBezierPath()
    var x: CGFloat = 0
    var y: CGFloat = 2
    var viewWidth = UIScreen.main.bounds.width
    var viewHeight = self.frame.height

    // here x, y, viewWidth, and viewHeight can be changed in
    // order to play around with the shadow paths.
    if (!top) {
        y+=(shadowRadius+1)
    }
    if (!bottom) {
        viewHeight-=(shadowRadius+1)
    }
    if (!left) {
        x+=(shadowRadius+1)
    }
    if (!right) {
        viewWidth-=(shadowRadius+1)
    }

    // selecting top most point
    path.move(to: CGPoint(x: x, y: y))
    path.addLine(to: CGPoint(x: x, y: viewHeight))
    path.addLine(to: CGPoint(x: viewWidth, y: viewHeight))
    path.addLine(to: CGPoint(x: viewWidth, y: y))
    path.close()
    self.layer.shadowPath = path.cgPath
  }

}

用法:

shadowview.addshadow(top: true, left: false, bottom: false, right: false) //shadowview is my UIView

谢谢。