如何在自定义类按钮上添加渐变?

时间:2019-05-05 06:58:02

标签: ios swift uibutton

我是Swift / Xcode的初学者。我在Xcode项目中创建了一个自定义类,以处理可以正常查找的按钮外观(颜色,形状等)。

但是,我无法成功为这些按钮添加渐变。

我尝试了下面的代码,但是它将新的平方渐变添加到我的圆形按钮上,而不是将预期的渐变应用于这些按钮。这是我已经尝试了很多的东西: -将此代码添加到我的自定义按钮类中(从UIButton继承) -将此代码添加到我的自定义按钮类中(从UIView继承) -以上2种方法并删除backgroungColor设置 -使用从viewController调用的单独函数

func ConfigMenuButtons() {

    // Set button shape color
    layer.cornerRadius = 37

    // Set button size
    translatesAutoresizingMaskIntoConstraints = false
    widthAnchor.constraint(equalToConstant: 74).isActive = true
    heightAnchor.constraint(equalToConstant: 74).isActive = true

    // Set button text
    setTitleColor(.white, for: .normal)
    titleLabel?.font = UIFont.boldSystemFont(ofSize: 15)

    // Set button shadow
    layer.shadowRadius = 2
    layer.shadowColor = UIColor.black.cgColor
    layer.shadowOffset = CGSize(width: 0, height: 2)
    layer.shadowOpacity = 0.8

    // Set button gradient colors
    let lightBlue = UIColor(red: 122/255, green: 127/255, blue: 249/255, alpha: 1)
    let darkBlue = UIColor(red: 74/255, green: 88/255, blue: 205/255, alpha: 1)

    // Set gradients
    let gradientLayer = CAGradientLayer()
    gradientLayer.frame = bounds
    gradientLayer.colors = [lightBlue.cgColor, darkBlue.cgColor]
    gradientLayer.locations = [0.0,1.0]
    gradientLayer.startPoint = CGPoint(x: 0.0, y: 0.0)
    gradientLayer.endPoint = CGPoint(x: 1.0, y: 1.0)

    clipsToBounds = true

    // insert gradients to button
    layer.insertSublayer(gradientLayer, at: 0)
}

但是我总是最终在目标按钮上出现此“方形渐变层”,而不是直接将这些渐变应用于这些按钮。 如果有人有任何建议,那就太好了。 谢谢!

Here is the screen shot of the button with its partial expected effect

1 个答案:

答案 0 :(得分:0)

Here is a screenshot of fixed issue

我终于解决了以下问题,添加了CGRect大小。

   func ConfigMenuButtons() {

    // Set button shape
    layer.cornerRadius = 37

    // Set button size
    translatesAutoresizingMaskIntoConstraints = false
    widthAnchor.constraint(equalToConstant: 74).isActive = true
    heightAnchor.constraint(equalToConstant: 74).isActive = true

    // Set button text
    setTitleColor(.white, for: .normal)
    titleLabel?.font = UIFont.boldSystemFont(ofSize: 15)

    // Set button shadow
    layer.shadowRadius = 2
    layer.shadowColor = UIColor.black.cgColor
    layer.shadowOffset = CGSize(width: 0, height: 2)
    layer.shadowOpacity = 0.8

    // Set button gradient colors
    let lightBlue = UIColor(red: 112/255, green: 117/255, blue: 239/255, alpha: 1)
    let darkBlue = UIColor(red: 70/255, green: 84/255, blue: 201/255, alpha: 1)

    // Set gradients
    let gradientLayer = CAGradientLayer()
    gradientLayer.frame = bounds
    gradientLayer.colors = [lightBlue.cgColor, darkBlue.cgColor]
    gradientLayer.locations = [0.0,1.0]
    gradientLayer.startPoint = CGPoint(x: 1.0, y: 0.5)
    gradientLayer.endPoint = CGPoint(x: 1.0, y: 1.0)

    // THAT IS THE LINE THAT COULD FIX THE ISSUE
    gradientLayer.frame = CGRect(x: 0, y: 0, width: 74, height: 74)

    // Set rounded shape
    gradientLayer.cornerRadius = 37

    // insert gradients to button
    layer.insertSublayer(gradientLayer, at: 0)
}