Ios 如何在UIView顶部设置阴影?

Ios 如何在UIView顶部设置阴影?,ios,swift,Ios,Swift,我已经尝试过了,但是没有得到结果。您的代码很好,但是您需要将负值指定为阴影偏移量,而不是.zero 为了更加方便,您可以为此创建扩展 请参考下面的代码 如何使用 试试这个:- viewName.addTopShadow(shadowColor: UIColor.gray, shadowOpacity: 0.9, shadowRadius: 10, offset: CGSize(width: 0.0, height : -5.0)) 下面的功能为我工作: override func viewDi

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

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

为了更加方便,您可以为此创建扩展

请参考下面的代码

如何使用

试试这个:-

viewName.addTopShadow(shadowColor: UIColor.gray, shadowOpacity: 0.9, shadowRadius: 10, offset: CGSize(width: 0.0, height : -5.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

    }
如何使用:

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
        }

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

另外,减小“阴影半径”的值,以便可以看到效果

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

你可以这样做:

actionButtonView.layer.shadowRadius = 3
用法:


谢谢。

您可以指定负阴影偏移,因此,添加actionButtonView.layer.shadowOffset=CGSizewidth:0,高度:-10
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)
actionButtonView.layer.shadowOffset = CGSize(width: 0, height: -3)
actionButtonView.layer.shadowRadius = 3
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