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

标签 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

最佳答案

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/

相关文章:

ios - 如何在 Nginx/Rails 上为 apple-app-site-association 文件设置正确的内容类型

ios - 如何正确设置 Core Data Stack 到 iOS 和 Swift 中的第一个 View Controller ?

ios - 核心数据保存和获取

ios 所有 UIButton 的全局触觉反馈

ios - addTarget 到自定义按钮 Swift 4

ios - UITextField EditingDidBegin 在 IOS 14.4 上触发两次

ios - 使用 UIScrollView 循环浏览图像

ios - 为回车键使用自定义名称

ios - 添加 UIPickerView 作为 subview ,很奇怪

ios - 点击动态创建的 UIButtons 崩溃应用程序