1

I want to set the background view for my tableview,

 override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    let backgroundView = BackgroundGradient(frame: self.view.frame)
    self.tableView.backgroundView = backgroundView
}

however the background remains white (it should be a blueish gradient). I have tried to set it in view did load, same result, I changed to background color to clear, and still nothing. what am I missing?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Le Marin
  • 135
  • 1
  • 12
  • Could you post the BackgroundGradient class? Setting a background colour at this point works, so probably an issue with the gradient. – darrenallen7 May 27 '17 at 11:44
  • Related: https://stackoverflow.com/q/30018035/1630618 – vacawama May 27 '17 at 11:53
  • https://pastebin.com/ZdSj9MhP this is the gradient class, it is working fine i use it for other viewcontrollers – Le Marin May 27 '17 at 12:03
  • As suggested by Badhan Ganesh, your cells have their own background color and are overlaying your tableview background. Use viewDidLoad() to setup your tableview or maybe even the new swifty way using a didSet{} on your tableview outlet (if you are not using UITableVIewController) – xxtesaxx May 27 '17 at 12:22

3 Answers3

3

Your UITableViewCell could be obscuring the backgroundView of your table. Try setting your cell's background color to clear.

Quote from Apple Docs (https://developer.apple.com/reference/uikit/uitableview/1614986-backgroundview)

You must set this property to nil to set the background color of the table view.

badhanganesh
  • 3,427
  • 3
  • 18
  • 39
  • i am trying to set the background view, if i set it to nil what's the point? – Le Marin May 27 '17 at 12:04
  • If you want to set background color, you need to set the view to nil. Besides, your background view could be hidden behind your cells. Is your tableView empty or filled fully with cells? – badhanganesh May 27 '17 at 12:08
  • i have emptied it for now, but now it looks like it is a problem with instanciating the view, because if i use a photo instead it works... – Le Marin May 27 '17 at 12:15
0

Try using:

self.tableView.backgroundView = UIImageView(image: UIImage(named: "backgroundImage.png"))

See if this works then there must be some issue with backgroundView

user3575114
  • 993
  • 7
  • 13
0

i found the problem... i had forgotten that UIColor(red:green:blue:alpha:) takes CGFloats between 0 and 1 instead of 0-255 as usualy, fixed it and it works fine now... Thanks all

Le Marin
  • 135
  • 1
  • 12