首页 > 解决方案 > 使用 CAGradientLayer 渲染按钮

问题描述

当我使用 CAGradientLayer 设置一个按钮时,它没有正确显示。

func setGradientBackground(color_A: UIColor, color_B: UIColor, indexLayer : Int){

    let granderLayer = CAGradientLayer()
    granderLayer.frame = bounds
    granderLayer.colors = [color_A.cgColor, color_B.cgColor]
    granderLayer.locations = [0.0, 1.0]
    granderLayer.startPoint = CGPoint(x: 1.0, y: 1.0)
    granderLayer.endPoint = CGPoint(x: 0.0, y: 0.0)

    layer.insertSublayer(granderLayer, at: UInt32(indexLayer))
}

在此处输入图像描述

在此处输入图像描述

在滚动后的 UITableView 的情况下,这有效

标签: swiftxcodebutton

解决方案


您应该检查以下行中的边界值:“granderLayer.frame = bounds”。


推荐阅读