【发布时间】:2018-02-16 12:25:22
【问题描述】:
我正在尝试将渐变添加到自定义表格单元格中的视图。但是,它的显示很奇怪,好像它没有从容器中获得正确的 CGRect。
首先,我将标签及其周围的框分别连接到属性标签和标签容器。
我已经定义了一个渐变函数:
func gradient(view: UIView) -> CAGradientLayer {
let gradient = CAGradientLayer()
gradient.frame = view.bounds
gradient.startPoint = CGPoint(x: 0, y: 0.5)
gradient.endPoint = CGPoint(x: 1, y: 0.5)
gradient.colors = [
UIColor.red.cgColor,UIColor.blue.cgColor]
return gradient
}
在 cellForRowAt 中,我有:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> ShoppingListCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "customShoppingListCell", for: indexPath) as! ShoppingListCell
cell.label.text = categories![indexPath.section].items.filter(predicate)[indexPath.row].title
cell.labelContainer.layer.insertSublayer(gradient(view: cell.labelContainer), at:0)
cell.selectionStyle = .none
return cell
}
但结果表如下所示:
这一切都有点奇怪和不一致。
【问题讨论】:
-
在
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath)方法中移动渐变代码 -
我已经试过了