【问题标题】:Apply Gradient Color in Tableview cell dynamically Swift [duplicate]在 Tableview 单元格中动态应用渐变颜色 Swift [重复]
【发布时间】:2017-11-10 07:45:37
【问题描述】:

我想用这种自定义渐变颜色动态填充表格视图单元格。

但无法弄清楚如何实现这一点。我对 Swift 编程比较陌生。任何形式的帮助都将是非常可观的。

谢谢

Please see the attached image

【问题讨论】:

  • 不要向单元格添加渐变,而是将其添加到 tableView 并确保 cellSpeartor 设置为清除颜色。那应该做的工作

标签: ios swift uitableview gradient uicolor


【解决方案1】:

希望这会有所帮助..

let layer = CAGradientLayer()
    layer.frame = self.tableview.frame
    let colorTop = UIColor(red: 104.0 / 255.0, green: 89.0 / 255.0, blue: 234.0 / 255.0, alpha: 1.0).cgColor
    let colorBottom = UIColor(red: 106.0 / 255.0, green: 206.0 / 255.0, blue: 176.0 / 255.0, alpha: 1.0).cgColor
    layer.colors = [colorTop, colorBottom]
    layer.startPoint = CGPoint(x:0.5, y:0.0)
    layer.endPoint = CGPoint(x:0.5, y:1.0);
    self.tableview.layer.addSublayer(layer)

注意:为单元格背景颜色设置清晰的颜色

【讨论】:

    【解决方案2】:

    试试这个在tableview中应用渐变

    let gradient = CAGradientLayer()
    
    gradient.frame = view.bounds
    gradient.colors = [UIColor.green.cgColor, UIColor.white.cgColor]
    
    self.tableView.separatorColor = UIColor.clear
    self.tableView.layer.insertSublayer(gradient, at: 0)
    

    并为单元格背景设置清晰的颜色

    cell.contentView.backgroundColor = UIColor.clear
    

    【讨论】:

    • 它为tableview添加了一个渐变。那太棒了。但是我的单元格内容被隐藏在渐变之下。我现在将您提供的代码放入 viewdidappear 函数中。有什么建议吗?
    • 你能告诉我你的代码吗?
    • 普里昂托·拉赫曼。您必须在 viewdidAppear 中针对 tableview 设置此渐变颜色,因为如果您在 viewdidLoad 中设置它,您的内容将被隐藏.. 此评论可能会帮助另一个陷入您的难题的人。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-04
    相关资源
    最近更新 更多