简体   繁体   中英

Swift - How to apply the drop shadow only on UIButton Bottom Shadow but not for its image and its title label?

I'm trying to add drop shadow to UIButton but I want to have only the shadow for UIButton bottom shadow only not for its image and title. I followed UIButton bottom shadow but it didn't work. Basically, here is what I'm having right now: 在此处输入图片说明

And here is what I want to have: 在此处输入图片说明

This is my current code:

button.layer.borderWidth = 0.5
button.layer.borderColor = UIColor.gray.cgColor
button.layer.shadowColor = UIColor.black.cgColor
button.layer.shadowOffset = CGSize(width: 0, height: 2)
button.layer.shadowOpacity = 1.0
button.layer.shadowRadius = 0
button.layer.masksToBounds = false

Please help. Thanks in advance.

检查附件图像中的输出

Checkout below lines of code

btn.setImage(UIImage(named: "Unknown.jpg"), for: .normal)

    btn.imageView?.layer.shadowColor = UIColor.blue.cgColor

    btn.imageView?.layer.shadowOffset = CGSize(width: 1, height: 1)

    btn.imageView?.layer.shadowOpacity = 1.0

    btn.imageView?.layer.shadowRadius = 5

    btn.imageView?.layer.masksToBounds = false

    btn.setTitle("    hello", for: .normal)

    btn.titleLabel?.layer.shadowColor = UIColor.black.cgColor

    btn.titleLabel?.layer.shadowOffset = CGSize(width: 1, height: 1)

    btn.titleLabel?.layer.shadowOpacity = 1.0

    btn.titleLabel?.layer.shadowRadius = 3

    btn.titleLabel?.layer.masksToBounds = false

    btn.backgroundColor = UIColor.red

Hi dear best way to apply shadow for transparent button you just need to embed your button in view and apply shadow effect on that view.

Like I have done below:

yourButtonView.layer.borderWidth = 0.5

yourButtonView.layer.borderColor = UIColor.gray.cgColor

yourButtonView.layer.shadowColor = UIColor.black.cgColor

yourButtonView.layer.shadowOffset = CGSize(width: 0, height: 2)

yourButtonView.layer.shadowOpacity = 1.0

yourButtonView.layer.shadowRadius = 0

yourButtonView.layer.masksToBounds = false

Please use the below extension for creating shadow

extension UIView {

func addshadow(top: Bool,
               left: Bool,
               bottom: Bool,
               right: Bool
               ) {

    let 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 = 0.3
    let path = UIBezierPath()
    var x: CGFloat = 0
    var y: CGFloat = 0
    var viewWidth = self.frame.width
    var viewHeight = self.frame.height
    if (!top) {
        y+=(shadowRadius+1)
    }
    if (!bottom) {
        viewHeight-=(shadowRadius+1)
    }
    if (!left) {
        x+=(shadowRadius+1)
    }
    if (!right) {
        viewWidth-=(shadowRadius+1)
    }
    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
}

}

use -

    button.addshadow(top: false, left: false, bottom: true, right: false)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM