0

I have an app that I'd like to enable split view on. The UI varies slightly for iPhone vs iPad (compact vs regular width).

In compact width mode, I have a UITableView with a list of items the user can select, and when they do I push a new View Controller onto the navigation stack. However, in regular width, the UITableView list is shown on the left, and then I have some other views to the right (not implemented in a UISplitViewController). So I've implemented these 2 different screens in 2 different ViewController classes.

If my user is using an iPad with regular width and then enters split view with my app and causes the app to change to compact width, I need to change which ViewController I'm showing to the user. What is the best strategy for this?

Note: I'm not using storyboard.

Thanks!

Wise Shepherd
  • 2,228
  • 4
  • 31
  • 43
  • 1
    Sounds like exactly the type of navigation `UISplitViewController` is designed to handle – dan Dec 04 '15 at 20:52

2 Answers2

0

There's 2 ways I see a solution to this. One, you can take a look at UISplitViewControllerDelegate, specifically the section on Collapsing and Expanding the Interface.

Another solution is to override your size collections so that the display is the same on iPhone and iPad, as in nothing collapses and expands, the two views are always "there". You can set the split view controller's preferredDisplayMode to Overlay which looks nice on iPhones. All you need to do is add

UITraitCollection* horizTrait = [UITraitCollection
                                 traitCollectionWithHorizontalSizeClass:UIUserInterfaceSizeClassRegular];
UITraitCollection* vertTrait = [UITraitCollection
                                traitCollectionWithVerticalSizeClass:UIUserInterfaceSizeClassRegular];
UITraitCollection* childTraits = [UITraitCollection
                                  traitCollectionWithTraitsFromCollections:@[horizTrait, vertTrait]];

[self setOverrideTraitCollection:childTraits forChildViewController:self.childViewControllers[0]];

to the parent class of your UISplitViewController (If there is no parent class, you must create one. This can just be a simple navigation controller)

Edit: I should mention that the above code simply sets the traits of the device to regular-regular for all devices.

Hayden Holligan
  • 1,822
  • 2
  • 19
  • 26
0

Thanks for the responses. I was thinking maybe I was supposed to be using state restoration, but then restoring a different View Controller depending on my trait collection.

Instead, I think I'll just create a view controller container and then, in willLayoutSubviews, I'll decide which view controllers to show depending on the current view's trait collections.

Thanks!

Wise Shepherd
  • 2,228
  • 4
  • 31
  • 43