0

I can't get my UITableView to change colors on start up with iOS 9.

I created a UITableView, which is a subview of my custom view, in code and set the background color during my subview's init method.

myTableView.backgroundColor = UIColor.yellowColor()

But it stayed white.

I used View Debugging > Capture View Hierarchy to confirm that it is the table view itself and not the table view cells that are white. (But I set the cells to clear anyway just in case).

I've seen a lot of older posts that talk about a similar problem (like this, this and this and this newer one) so I tried things like

myTableView.backgroundView = nil // or UIView()

or

myTableView.backgroundView.backgroundColor = UIColor.clearColor()

but these also didn't work.

I discovered that if I did

myTableView.backgroundColor = UIColor.yellowColor()

sometime after the original layout (for example, on a button tap), then it would change the background color.

This is all only a problem in iOS 9.3. In my tests with iOS 8.1 and 8.4 I had no problems setting the background color during init.

Is there anything else that I can try?

Community
  • 1
  • 1
Suragch
  • 364,799
  • 232
  • 1,155
  • 1,198
  • `init` method is not a good place to modify UI properties. Try moving it to `layoutSubviews` method – redent84 May 06 '16 at 13:29
  • Where in your code are you adding this tableView? viewDidLoad by any chance? If so, move your code to viewWillAppear, and use the initWithFrame method, instead of init. Init is derives from NSObject, whilst initWithFrame is specifically used for UIView objects. – Sander Saelmans May 06 '16 at 13:32
  • 1
    Try to set background color in `awakeFromNib` – Bing May 06 '16 at 13:36
  • It sounds like you also might be setting it on a background tread, and the button tap kicks off the update on the main thread. Can you put the method where you set the background? Also, great places to do this are in viewDidLoad, and viewWillAppear. – Siriss May 06 '16 at 13:37
  • @redent84, You were correct. Setting the background color in `layoutSubviews` rather than `init` solved the problem. If you write this as an answer I will accept it. – Suragch May 06 '16 at 13:42

3 Answers3

3

init method is not a good place to modify UI properties because the view hierarchy hasn't been constructed yet.

Try using layoutSubviews or didMoveToWindow for UIView subclasses:

var customColor = UIColor.yellowColor()
override func layoutSubviews() {
    super.layoutSubviews()
    self.backgroundColor = customColor
}

override func didMoveToWindow() {
    super.didMoveToWindow()
    self.backgroundColor = customColor
}

You can also use viewDidLoad or awakeFromNib methods in UIViewController subclasses:

override func viewDidLoad() {
    super.viewDidLoad()
    customTableView.backgroundColor = UIColor.yellowColor()
}

override func awakeFromNib() {
    super.awakeFromNib()
    customTableView.backgroundColor = UIColor.yellowColor()
}
redent84
  • 17,763
  • 4
  • 53
  • 83
1

Looks like a bug others are also experiencing. As a workaround, i used UIImageView (with the color i wanted) and assigned it to the uitableview background. Hope it works for you too!

 tableView.backgroundView = UIImageView(image: UIImage(named: "yellowColorImage"))
Harris
  • 312
  • 3
  • 13
0

I'm not sure that this is the best answer but it works. I delayed setting the color until the next run loop by doing this in the init method:

dispatch_async(dispatch_get_main_queue()) {
    self.myTableView.backgroundColor = UIColor.clearColor()
}

Whatever it is in the system that is setting the background to white gets set back now.

Suragch
  • 364,799
  • 232
  • 1,155
  • 1,198