3
votes

I am designing an iPad app using a UISplitViewController. I have configured the UISplitViewController such that the master view controller (i.e. the thinner view on the left-hand side) is always visible via splitViewController.preferredDisplayMode = .AllVisible.

The detail view controller is a UICollectionView and I would like to change the way it displays depending on whether the iPad is portrait or landscape. If it's portrait, I will make the collection view only display a single column (similar to a UITableView). If it's landscape, with more horizontal screen real estate available, then I will display multiple columns. I figured I could do this with a few conditionals within the class which implements the data source and delegate for that collection view.

I don't want to just check the orientation of the device, as I would prefer to use size classes and traits so that the app could also work on iPhone, etc. (when the same traits are encountered).

I know that each view controller has a traitCollection property, but regardless of the initial orientation of the iPad when I print out this property in the viewDidAppear method it always says that the view controller is _UITraitNameHorizontalSizeClass = Compact, _UITraitNameVerticalSizeClass = Regular.

I would expect this if the iPad was portrait, but even when it's initially landscape this is printed out - although I would expect that orientation to be Regular and Regular for the horizontal and vertical size classes.

Additionally, if I implement the traitCollectionDidChange: method in the view controller it's never called on rotation or even if the expand button on the detail view controller is tapped to hide the master view controller (and give the detail view controller the full screen).

So, my question is how can I detect size class / trait changes in the view controllers within a split view controller...or am I doing this completely wrong?

For the purpose of any sample code in responses, the app is being written in Swift targeting iOS 9.

Thanks in advance for any help!

1
hi there, were you able to find a solution to this? - vinnybad
Not yet unfortunately, but I am going to do some more work on this next week. - Skoota

1 Answers

0
votes

Maybe you can try to check traitCollection of self.splitViewController using the following method:

private var isCompactOrientation: Bool {
    get {
        return self.splitViewController?.traitCollection.horizontalSizeClass == UIUserInterfaceSizeClass.Compact
    }
}

If using self.traitCollection.horizontalSizeClass, it always return .Compact.