I'm trying to test some size-class specific traits in my unit tests, but am running into an issue where the size class is not properly set in awakeFromNib
. When I run the app, it is set correctly by inspecting it in LLDB after everything is on screen, but I can never seem to trigger the size class being set while doing unit tests.
Does anyone have any information about when the trait collection is set / how to trigger it?
Edit: For clarity, these are unit tests which are testing just the view, not the ViewController it's in. So I'm loading the view from it's nib, then performing the tests on it.
I've tried calling layoutSubviews()
hoping that would trigger some sort of size-class setting, but it did not work. When I place breakpoints in traitCollectionDidChange
the stack trace only has main
in it, so I'm not sure what's actually triggering it.
I figured it out. The key is that the view needs to be part of a window in order to have it's size class updated, so the following code remedied the situation:
let window = UIWindow()
// "view" is the view you want the size class updated one
window.addSubview(view)