Reputation: 1419
I have a universal app that uses two different UISplitview Controllers to control two different Master/Detail sets of information (one data type is nested within the other).
I switch from the first into the other by calling the following function:
@IBAction func viewEntries(sender: AnyObject) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let entriesSplitView = storyboard.instantiateViewControllerWithIdentifier("entriesSplitView") as! UISplitViewController
entriesSplitView.delegate = self.appDelegate
UIView.transitionWithView(self.view.window!, duration: 0.25, options: .TransitionCrossDissolve, animations:
{
self.appDelegate.window?.rootViewController = entriesSplitView
}, completion: nil)
}
In the master view controller of this secondary UISplitView, I set the navigation controller's leftBarButton to trigger a similar function to return to the original, most top level UISplitView.
My only issue is that I want to make sure that on the iPhone, the first view that shows up after this transition is the detail view controller, but I want the master ViewController first, and then to be able to view the detail after that. I can solve that by adding:
let masterNavigationController = entriesSplitView.viewControllers[0] as! UINavigationController
masterNavigationController.popToRootViewControllerAnimated(false)
after self.appDelegate.window?.rootViewController = entriesSplitView
, but when I do, there is a quick flicker. If I put it before that call, it doesn't have the desired effect.
Any ideas on the proper way to make sure that the master view is the initial one called, while also allowing access to the detail view?
Upvotes: 0
Views: 540
Reputation: 2892
I think UISplitViewControllerDelegate is your friend here.
Make your root view controller the delegate for the split view controller:
self.splitViewController?.delegate = self
And then implement the following method:
extension MyViewController : UISplitViewControllerDelegate {
func splitViewController(_ splitViewController: UISplitViewController, collapseSecondary secondaryViewController: UIViewController, onto primaryViewController: UIViewController) -> Bool {
return true
}
}
This will force the split view controller to display the master by default in horizontally compact environments. You may need custom logic in the body of the function to create the desired effect for your app, but this is the place to do it.
Upvotes: 0