UIButton bottom shadow

后端 未结 11 1163
-上瘾入骨i
-上瘾入骨i 2020-12-12 14:14

I have a UIButton which is very similar to the standard iOS keyboard alphabet button.

I am not sure how to create a shadow only for the bottom layer li

11条回答
  •  鱼传尺愫
    2020-12-12 14:43

    To add shadow to a button with corner radius:

    final class CustomButton: UIButton {
    
        private var shadowLayer: CAShapeLayer!
    
        override func layoutSubviews() {
            super.layoutSubviews()
    
            if shadowLayer == nil {
                shadowLayer = CAShapeLayer()
                shadowLayer.path = UIBezierPath(roundedRect: bounds, cornerRadius: 33).cgPath
                if self.backgroundColor != nil {
                    shadowLayer.fillColor = self.backgroundColor?.cgColor
                }
                else{
                    shadowLayer.fillColor = UIColor.white.cgColor
                }
                shadowLayer.shadowColor = UIColor.gray.cgColor
                shadowLayer.shadowPath = shadowLayer.path
                shadowLayer.shadowOffset = CGSize(width: 0.0, height: 3.0)
                shadowLayer.shadowOpacity = 0.4
                shadowLayer.shadowRadius = 2
    
                layer.insertSublayer(shadowLayer, at: 0)
    
            }
    
        }
    
    }
    

提交回复
热议问题