1

I want to populate tableview cell with this type of custom gradient color dynamically.

But can't figure out how to achieve this. I am kind of fresher in Swift programming. Any kind of help will be highly appreciable.

Thanks

Please see the attached image

2 Answers2

0

Hope this will help..

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)

Note: Set clear colour to cell backgroundcolour

Bapu
  • 94
  • 5
0

Try this to apply gradient in 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)

And set clear color to cell background

cell.contentView.backgroundColor = UIColor.clear
Priya
  • 735
  • 5
  • 10
  • Its adding a gradient to the tableview. Thats great. But my cell contents are getting hidden under the gradient. I am putting the code you provided in viewdidappear function now. Any suggestion? –  Nov 10 '17 at 08:22
  • Can you show me your code? – Priya Nov 10 '17 at 09:44
  • Priyonto Rahman . you have to set this gradient color against tableview in viewdidAppear because if u set it in viewdidLoad your content will be hidden .. this comment May help another person who will be stuck in your preoblem . – moahmed ayed Jan 23 '19 at 07:23