rikkigibson rikkigibson - 2 months ago 5
iOS Question

How do I check the current size class for the screen from within a nested child view controller?

I have a screen in my app with multiple child view controllers. On smaller screen sizes like iPhone, the view controllers are placed flush against each other. On iPad screen sizes, one of the view controllers floats on top of the other.

I want to make it so my view controller that sometimes floats only has a corner radius when it's floating. Therefore, I've looked into overriding a method like

in my child view controller to set the corner radius of its view based on whether its size class is wRegular,hRegular, which is for iPads in portrait or landscape.

However, the child view controller doesn't seem to have the same size class values set as the root view controller, and traveling up the
hierarchy doesn't seem to get me to a view controller that does have those values.

How should I access the current size class of the top of the view hierarchy from a child view controller?


Like this (this is current Swift 3):

let tc = UIScreen.main.traitCollection

Now you can interrogate the vertical and horizontal size class of tc.