简体   繁体   English

Swift-如何仅将下拉阴影应用于UIButton底部阴影,而不应用于其图像和标题标签?

[英]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. 我试图将阴影添加到UIButton,但我只希望UIButton底部阴影不包含其图像和标题。 I followed UIButton bottom shadow but it didn't work. 我跟随UIButton底部阴影,但是没有用。 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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