how to gradient a background of a tableview in swift

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



how to gradient a background of a tableview in swift


func setTableViewBackgroundGradient(sender: UITableViewController, _ topColor:UIColor, _ bottomColor:UIColor)

let gradientBackgroundColors = [topColor.cgColor, bottomColor.cgColor]
let gradientLocations = [0.0,1.0]

let gradientLayer = CAGradientLayer()
gradientLayer.colors = gradientBackgroundColors
gradientLayer.locations = gradientLocations as [NSNumber]

gradientLayer.frame = sender.tableView.bounds
let backgroundView = UIView(frame: sender.tableView.bounds)
backgroundView.layer.insertSublayer(gradientLayer, at: 0)
sender.tableView.backgroundView = backgroundView





What exactly is the problem? Please read How to Ask and update your question.
– Ashley Mills
Aug 31 at 9:17




1 Answer
1



Try this it's working for me (Swift 4)



Note : First of all set Tableview Cell & Cell ContentView Background
Color to Clear Color.


@IBOutlet fileprivate weak var tblVw: UITableView!

private var gradientLayer = CAGradientLayer()

override func viewDidLoad()
super.viewDidLoad()


override func viewDidLayoutSubviews()
super.viewDidLayoutSubviews()

if gradientLayer.superlayer != nil
gradientLayer.removeFromSuperlayer()

let topColor = UIColor(red: 16.0/255.0, green: 12.0/255.0, blue: 54.0/255.0, alpha: 1.0)
let bottomColor = UIColor(red: 57.0/255.0, green: 33.0/255.0, blue: 61.0/255.0, alpha: 1.0)
gradientLayer.startPoint = CGPoint(x: 0.0, y: 0.0)
gradientLayer.endPoint = CGPoint(x: 1.0, y: 1.0)
gradientLayer.colors = [topColor.cgColor, bottomColor.cgColor]
gradientLayer.frame = tblVw.bounds
let backgroundView = UIView(frame: tblVw.bounds)
backgroundView.layer.insertSublayer(gradientLayer, at: 0)
tblVw.backgroundView = backgroundView



Output:



enter image description here





sir it doesn't work it hides all content of table view
– abhishek rajput
Aug 30 at 13:28





@abhishekrajput you missed someone in integration because it's working file please check it one again.
– Nikunj Kumbhani
Aug 31 at 5:00





Dear Sir, i check it again. color hides the content of tableview . sir can you send me a project build so i can check it. email: rajput.abhishek660@gmail.com
– abhishek rajput
Aug 31 at 6:45





@abhishekrajput You needs to set light dark color combination for component and background color. See the updated answer.
– Nikunj Kumbhani
Aug 31 at 9:17





@abhishekrajput Stack Overflow isn't a code writing service. If you've tried something, please update your question to show what you've tried and what the problem is.
– Ashley Mills
Aug 31 at 9:19






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS