ios - 带有背景图像、圆角和阴影的 UIButton

标签 ios swift calayer shadow rounded-corners

我正在尝试创建一个带有圆角、背景图像和阴影的 UIButton。在添加阴影之前,一切正常。

enter image description here

但是添加阴影值后,阴影不显示。显然是由于 clipsToBounds 属性值设置为 true。如果我删除它,它看起来像这样。

enter image description here

由于我还需要圆角半径,因此我不能让 clipsToBoundsfalse

这是我的代码。

class CustomButton: UIButton {
    
    var cornerRadius: CGFloat {
        get {
            return layer.cornerRadius
        }
        set {
            layer.cornerRadius = newValue
            clipsToBounds = true
        }
    }
    
    var shadowRadius: CGFloat {
        get {
            return layer.shadowRadius
        }
        set {
            layer.shadowRadius = newValue
        }
    }
    
    var shadowOpacity: Float {
        get {
            return layer.shadowOpacity
        }
        set {
            layer.shadowOpacity = newValue
        }
    }
    
    var shadowOffset: CGSize {
        get {
            return layer.shadowOffset
        }
        set {
            layer.shadowOffset = newValue
        }
    }
    
    var shadowColor: UIColor? {
        get {
            if let color = layer.shadowColor {
                return UIColor(cgColor: color)
            }
            return nil
        }
        set {
            if let color = newValue {
                layer.shadowColor = color.cgColor
            } else {
                layer.shadowColor = nil
            }
        }
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        
    }
    
}


private lazy var button: CustomButton = {
    let button = CustomButton()
    button.translatesAutoresizingMaskIntoConstraints = false
    button.setBackgroundImage(UIImage(named: "Rectangle"), for: .normal)
    button.setTitleColor(.white, for: .normal)
    button.setTitle("Sign Up", for: .normal)
    button.titleLabel?.font = UIFont.systemFont(ofSize: 15, weight: .semibold)
    button.cornerRadius = 20
    button.shadowColor = .systemGreen
    button.shadowRadius = 10
    button.shadowOpacity = 1
    button.shadowOffset = CGSize(width: 0, height: 0)
    return button
}()

是否有解决方法可以同时具有阴影和圆角半径?

Demo project

最佳答案

您可以通过添加不同图层的阴影和背景图像来实现。

首先,如果您不需要这些属性,请删除所有属性并修改您的 CustomButton 实现,如下所示(根据您的需要进行修改):

class CustomButton: UIButton {
    
    private let cornerRadius: CGFloat = 20
    private var imageLayer: CALayer!
    private var shadowLayer: CALayer!
    
    override func draw(_ rect: CGRect) {
        addShadowsLayers(rect)
    }
    
    private func addShadowsLayers(_ rect: CGRect) {
        // Add Image
        if self.imageLayer == nil {
            let imageLayer = CALayer()
            imageLayer.frame = rect
            imageLayer.contents = UIImage(named: "Rectangle")?.cgImage
            imageLayer.cornerRadius = cornerRadius
            imageLayer.masksToBounds = true
            layer.insertSublayer(imageLayer, at: 0)
            self.imageLayer = imageLayer
        }
        
        // Set the shadow
        if self.shadowLayer == nil {
            let shadowLayer = CALayer()
            shadowLayer.masksToBounds = false
            shadowLayer.shadowColor = UIColor.systemGreen.cgColor
            shadowLayer.shadowOffset = .zero
            shadowLayer.shadowOpacity = 1
            shadowLayer.shadowRadius = 10
            shadowLayer.shadowPath = UIBezierPath(roundedRect: rect, cornerRadius: cornerRadius).cgPath
            layer.insertSublayer(shadowLayer, at: 0)
            self.shadowLayer = shadowLayer
        }
    }
}

并初始化您的按钮,如下所示:

private lazy var button: CustomButton = {
    let button = CustomButton()
    button.translatesAutoresizingMaskIntoConstraints = false
    button.setTitleColor(.white, for: .normal)
    button.setTitle("Sign Up", for: .normal)
    button.titleLabel?.font = UIFont.systemFont(ofSize: 15, weight: .semibold)
    return button
}()

enter image description here

关于ios - 带有背景图像、圆角和阴影的 UIButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62741293/

相关文章:

ios - 如何从表示层创建新的 CALayer?

ios - Apache Cordova Visual Studio 2015 Remotebuild iOS 版本 > Http 404 : Installation failed: Check your provisioning profile

ios - 将 4 个数字的字符串解析为 CGRect

iOS utf-8 编码问题

swift - ChannelInboundHandler (Swift-NIO) 中的 writeDataUnsupported

ios - 如何禁用 UITabBarController

ios - 缩放 CALayer 内容的大小

ios - 如何在 didEnterBackground 后继续 UIImageView 动画?

ios - 无法在选择器 View 中设置组件宽度

ios - 一个 WebView 的多个 UIButton