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.
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:
You have:
Put ContentView like Default (transparent) in SB, and in your Cell's Method in awakeFromNib, just:
self.backgroundColor = UIColor.SomeColor
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
cell.contentView.backgroundColor = UIColor.yellowColor()
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cell.contentView.backgroundColor = UIColor.yellow
}
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
}
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!
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()