4

I'm using probably a little bit exotic way of initialization of my UI components. I create them programmatically and among them is a UITableView instance, I set its background color immediately upon initialization, like this:

class MyViewController: UIViewController {
    ...
    let tableView = UITableView().tap {
         $0.backgroundColor = .black
         $0.separatorStyle = .none
    }
    ...
}

where tap is extension function:

func tap(_ block: (Self) -> Void) -> Self {
    block(self)
    return self
}

This worked very well in my previous project which was created in Xcode 8 and then migrated to Xcode 9 without breaking anything. But now I've created brand new project in Xcode 9 and copy-pasted above-mentioned extension to it, but seems like something went wrong. When my view controller appears on screen table has white background and default separator insets.

This seems to affect only some of the properties because others are working as they should have (e.g. $0.register(nib: UINib?, forCellReuseIdentifier: String) registers required cell class and $0.showsVerticalScrollIndicator = false hides scroll indicator).

Perhaps some of you, guys, could give me an idea what's the heart of the matter.

Here's full code, to reproduce the issue simply create a new project and replace ViewController.swift's content. As you can see, table has correct rowHeight (160) but resets its background color.

As for "before view appears" statement: I've printed table's background color in viewDidLoad, viewWillAppear and viewDidAppear like this:

print(#function, table.backgroundColor.debugDescription)

– it changes its color only in the last debug print:

viewDidLoad() Optional(UIExtendedGrayColorSpace 0 1)
viewWillAppear Optional(UIExtendedGrayColorSpace 0 1)
viewDidAppear Optional(UIExtendedSRGBColorSpace 1 1 1 1)
Dan Karbayev
  • 2,539
  • 2
  • 16
  • 27

1 Answers1

1

I ended up moving the initialization to lazy var's function – turns out initializing UITableView during the initialization of it's view controller has some side effects.

Dan Karbayev
  • 2,539
  • 2
  • 16
  • 27