我是Swift/Xcode的初学者。我在Xcode项目中创建了一个自定义类,用于处理works-find中的按钮外观(颜色、形状等)。
但是,我无法成功地为这些按钮添加渐变。
我尝试了下面的代码,但是它在圆形按钮上添加了一个新的方形渐变,而不是将预期的渐变应用到那些按钮上。我已经试了很多次了:
-将此代码添加到自定义按钮类(从UIButton继承)
-将此代码添加到自定义按钮类(从UIView继承)
-以上两种方法并删除背景颜色设置
-使用从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

最佳答案

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

关于ios - 如何在自定义类按钮上添加渐变?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55989585/

10-12 14:35
查看更多