keverly
keverly

Reputation: 1430

Set background color of UITableView programmatically not working

I have added a UIView using storyboard and subclassing it. Within this view, I am adding a UITableView programmatically. Here is the code to create the tableview and add it:

private func commonInit() {
    self.backgroundColor = .clear

    self.categoryTableView = UITableView(frame: CGRect(x: 0, y: 0, width: self.frame.width, height: self.frame.height))

    categoryTableView?.delegate = self
    categoryTableView?.dataSource = self

    self.categoryTableView?.backgroundView = nil
    self.categoryTableView?.backgroundColor = .yellow

    self.categoryTableView?.isScrollEnabled = false
    self.categoryTableView?.allowsMultipleSelection = true

    self.addSubview(categoryTableView!)
}

This is what it looks like. I'm expecting the background of the tableview to be yellow (it is white in the screenshot)

I have also set the cells background color to clear which seems to be working. When I look at the UI hierarchy, it is pretty obvious that the White is coming from the the tableview.

I feel like this should be really strait forward. The strange part is the lines self.categoryTableView?.isScrollEnabled = false and self.categoryTableView?.allowsMultipleSelection = true both seem to be working, but the background color changing isn't.

Upvotes: 2

Views: 1994

Answers (2)

luizv
luizv

Reputation: 638

I got the exactly same problem and found that just changing the tableView.backgroundColor after tableView.backgroundView = nil doesn't work.

My case also was a programmatically created UITableView inside an UIView.

The solution is to add a backgroundView to tableView and change the property backgroundColor of backgroundView object.


Swift 5 / iOS 12.x

Changing tableView background color

For any color other than .clear, the above should work:

    self.tableView.backgroundView = UIView() //Create a backgroundView
    self.tableView.backgroundView!.backgroundColor = .lightGray //choose your background color

Changing tableViewCells background color

Going a little further, some may find that the tableView background color didn't appear as intended because of the background colors of the UITableViewCell instances. A simple solution to make sure the cells have a transparent background:

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

Upvotes: 2

David S.
David S.

Reputation: 6705

I tested this in a playground, and the result was as expected.

import UIKit
import XCTest
import PlaygroundSupport


let view = UIView(frame: CGRect(x: 0, y: 0, width: 500, height: 500))
PlaygroundPage.current.liveView = view

view.backgroundColor = UIColor.blue

let tableView = UITableView(frame:CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height))
tableView.backgroundColor = UIColor.yellow
view.addSubview(tableView)

The result:

colored tableview

My guess is something else is afoot here. Maybe something with the sizing of the frames, or where you are calling the commonInit() method?

Upvotes: 0

Related Questions