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
}
Asked
Active
Viewed 1,135 times
-3

Nirav D
- 71,513
- 12
- 161
- 183

abhishek rajput
- 42
- 7
-
What exactly is the problem? Please read [ask] and update your question. – Ashley Mills Aug 31 '18 at 09:17
1 Answers
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:

Nikunj Kumbhani
- 3,758
- 2
- 26
- 51
-
-
@abhishekrajput you missed someone in integration because it's working file please check it one again. – Nikunj Kumbhani Aug 31 '18 at 05: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 '18 at 06:45
-
@abhishekrajput You needs to set light dark color combination for component and background color. See the updated answer. – Nikunj Kumbhani Aug 31 '18 at 09:17
-
1@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 '18 at 09:19
-
Actually sir. cell label color is set to black that's why it was not visible. thanks again sir. @NikunjKumbhani – abhishek rajput Aug 31 '18 at 11:21
-
@AshleyMills actually sir i am a fresher that's why i did this. from next time i'll keep this thing on mind thankyou sir – abhishek rajput Aug 31 '18 at 11:22
-
@NikunjKumbhani sir i want to ask one thing . Can you help me ?? – abhishek rajput Sep 05 '18 at 11:30
-
-
sir actually i used https://github.com/dekatotoro/SlideMenuControllerSwift but when i used slide menu. the contains of mainscreen is showing to another screen – abhishek rajput Sep 05 '18 at 12:15
-
@abhishekrajput Use this one it's working fine by just using two classes no need to add framework and pod install. https://stackoverflow.com/a/51725803/10150796 – Nikunj Kumbhani Sep 05 '18 at 12:26
-
actually sir in this. Screen is shifted to right and i have to implement that type of slide menu (Slide Menu In). so sir help me in this – abhishek rajput Sep 05 '18 at 12:31
-
@NikunjKumbhani sir https://drive.google.com/drive/folders/12maQmHJTju2-gHwd8BPIQGlwc35l42r7?usp=sharing – abhishek rajput Sep 05 '18 at 12:40
-
@abhishekrajput Check properly you missed something in integration. – Nikunj Kumbhani Sep 05 '18 at 12:48
-
Dear, it's not possible that I write code for you, You need to check it from your end. – Nikunj Kumbhani Sep 05 '18 at 12:53
-
@NikunjKumbhani sir i have tried many times. but i have same problem again and again – abhishek rajput Sep 05 '18 at 12:54
-
sir actually i shared a google drive link can you check it sir. i don't want you to write. but sir please find out the issues. – abhishek rajput Sep 05 '18 at 12:55
-
@abhishekrajput Not able to found storyboard in your folder some files are missing and it's not possible you can download the demo from GitHub and integrate carefully according to that you got the result. – Nikunj Kumbhani Sep 05 '18 at 12:58
-
@NikunjKumbhani . https://drive.google.com/open?id=1Z-TKpaYqJuG-3aPXnQUtD3YPXUABq0XF – abhishek rajput Sep 05 '18 at 13:12
-
@abhishekrajput Right now I just change **let slideMenuController = ExSlideMenuController(mainViewController:nvc, leftMenuViewController: rightViewController, rightMenuViewController: leftViewController)** in Appdelegate file of Github demo project and chek it's workig completely that means you missed something in integration so Create new project and do all things step by step same as demo project. – Nikunj Kumbhani Sep 05 '18 at 13:36
-
-
@abhishekrajput I say that change it in demo project's app delegate file, not in your project. – Nikunj Kumbhani Sep 05 '18 at 13:40