【问题标题】:Swift - How to apply the drop shadow only on UIButton Bottom Shadow but not for its image and its title label?Swift - 如何仅在 UIButton 底部阴影上应用投影,而不是在其图像和标题标签上应用投影?
【发布时间】:2019-04-08 10:27:39
【问题描述】:

我正在尝试向 UIButton 添加阴影,但我只想为 UIButton 底部阴影添加阴影,而不是为其图像和标题添加阴影。我关注了UIButton bottom shadow,但没有成功。 基本上,这就是我现在所拥有的:

这就是我想要的:

这是我当前的代码:

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

请帮忙。提前致谢。

【问题讨论】:

  • 看起来您的按钮具有透明背景,将其背景更改为白色应该可以解决问题。

标签: ios swift uibutton uikit


【解决方案1】:

您好亲爱的为透明按钮应用阴影的最佳方法,您只需将按钮嵌入视图并在该视图上应用阴影效果。

就像我在下面所做的那样:

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

【讨论】:

    【解决方案2】:

    请使用以下扩展来创建阴影

    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
    }
    
    }
    

    使用 -

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

    【讨论】:

      【解决方案3】:

      检查下面的代码行

      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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-01-24
        • 2013-07-04
        • 1970-01-01
        • 2015-01-20
        • 2018-06-20
        • 2015-11-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多