Vikas Seth
Vikas Seth

Reputation: 373

iOS Swift - How to change background color of Table View?

I have a simple table view, I can change the colors of cells, but when trying to change the color of Table View (Background part) it is not working... I tried it via Storyboard... Can someone please help

Upvotes: 37

Views: 86361

Answers (6)

William
William

Reputation: 271

You have:

  1. Cell background AND
  2. Cell ContentView

Put ContentView like Default (transparent) in SB, and in your Cell's Method in awakeFromNib, just:

self.backgroundColor = UIColor.SomeColor

Upvotes: 1

Brigitte Alese
Brigitte Alese

Reputation: 11

I'm a little late to the game! But, none of the above answers worked for me, so just going to share, what I found worked, in case anyone else is jumping on this thread.

NOTE: I also have my own custom cell class. Not sure if that will affect your code!

@IBDesignable class YourViewController: UITableViewController {

//IBInspectable and IBDesignable makes the properties accessible in storyboard
@IBInspectable var tableViewBackground: UIColor?
@IBInspectable var cellViewBackground: UIColor?

//in case you want to customize the text color, as well!
@IBInspectable var customTextColor: UIColor?


override func viewDidLoad() {
//your code
self.tableView.backgroundColor = tableViewBackground
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath:    IndexPath) -> UITableViewCell {

    let cell = UITableViewCell()
    cell.textLabel?.textColor = customTextColor
    cell.backgroundColor = cellViewBackground
    return cell
}
//the rest of your code
}

Finding your inspectables:

1). In the Storyboard menu for the viewController of your tableView, click on yourViewController. The first dropdown inside the viewController. (The first dropdown will contain your tableView and cellView. It can also contain other goodies depending on your particular project).

2). In the attributes inspector, you'll see a heading with the name of your viewController and the @IBInspectable properties we defined above!!

3). You can then change them however you want there.

Hope this helps!

Upvotes: 1

IOS Singh
IOS Singh

Reputation: 617

use this code for change tableView color

override func viewDidLoad() {
        super.viewDidLoad()

       // define tableview background color
        self.tableView.backgroundColor = UIColor.clearColor()
}

for change tableView cell color

cell.backgroundColor = UIColor.clearColor()

Upvotes: 5

Homam
Homam

Reputation: 5076

First set the background color of the tableView in viewDidLoad like below:

override func viewDidLoad() {
    super.viewDidLoad()   
    self.tableView.backgroundColor = UIColor.lightGrayColor()
}

Now add this method:

override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    cell.backgroundColor = UIColor.clearColor()
}

In Swift 3, use below methods instead of above one:

override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView.backgroundColor = UIColor.lightGray
}

override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    cell.backgroundColor = UIColor.clear
}

Upvotes: 72

Lucas Farah
Lucas Farah

Reputation: 1102

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    cell.contentView.backgroundColor = UIColor.yellowColor()
}

Swift 3

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    cell.contentView.backgroundColor = UIColor.yellow
}

Upvotes: 17

user6082493
user6082493

Reputation: 71

If you want to achieve that via storyboard then select "Content View" of "table view cell" in your tableView then in attribute Inspector go to View and change its color like so:

Xcode

Upvotes: 7

Related Questions