UISplitViewController - Deviation / Surfacing Detailed view of the controller in code in sleep mode

With iOS8, we are allowed to use the UISplitViewController on both compact and conventional devices. This is great because I don't need to create two different storyboards for the iPhone and iPad, but there is one problem I'm stuck with.

If the split-image controller is on the iPad (if the zeroed property is NO), I can simply call this to show MasterVC on the left side.

self.splitViewController.preferredDisplayMode = UISplitViewControllerDisplayModePrimaryOverlay; [self.splitViewController.displayModeButtonItem action]; 

But if it is on the iPhone (if the compensated property is YES), displayMode is ignored and does nothing.

I cannot put DetailVC in popToRootViewControllerAnimated because DetailVC has its own navigation controller.

How does Apple expect us to show MasterVC (release DetailVC) in code in sleep mode if there is no method like dismissViewControllerAnimated:completion: for the view controller that was presented with showDetail? Your help will be greatly appreciated. Thanks

+10
source share
4 answers

On devices that do not support split mode, if

  • You want to present the main view controller instead of the part, when the UISplitViewController loaded first and then returns YES to its delegate class ( UISplitViewControllerDelegate ) UISplitViewControllerDelegate method method splitViewController:collapseSecondaryViewController:ontoPrimaryViewController: should do this:

     - (BOOL)splitViewController:(UISplitViewController *)splitViewController collapseSecondaryViewController:(UIViewController *)secondaryViewController ontoPrimaryViewController:(UIViewController *)primaryViewController { return YES; } 
  • You want to reject the detail view controller back to the master after a certain event (for example, clicking a button). In this case, you need to press the navigation controller of the part controller:

     [detailViewController.navigationController.navigationController popToRootViewControllerAnimated:YES] 
+19
source

We had a similar problem trying to break out of a detailed view in a controller with a shared view.

Although I'm sure that the accepted answer works fine, another approach that I found works, and maybe a little cleaner, is to use unwinding.

I set up the spread on the main view I wanted to return to, then created a segue link to expand the segue from the view I wanted to pop (note: assumes you are using storyboards).

Make sure you set IBAction in the destination window that appears, which you are returning:

 -(IBAction)prepareForUnwind:(UIStoryboardSegue *)segue { } 

Connect the output to the segment in the storyboard to unwind. Sorry, I do not provide a lot of details on how to configure segue promotion, but many tutorials are available for this.

Then on your controller that you want to reject, connect segue to the expanding segment of the controller that you are returning to. Be sure to name segue.

Then when you click on the view controller you want to remove, just call

 [self performSegueWithIdentifier:@"unwindSegueName" sender:self]; 

This worked very well, and it does not allow digging back into the navigation hierarchy, which may change.

Hope this is helpful to someone! Happy Holidays!

+6
source

Thanks pNre ! Here is the code that will handle the display of the custom back button when displayModeButton , and displayModeButton when it doesn't crash.

 lazy var backButtonItem: UIBarButtonItem = { UIBarButtonItem(image: UIImage(named: "backImage"), style: .plain, target: self, action: #selector(dismissAnimated)) }() override func viewWillLayoutSubviews() { super.viewWillLayoutSubviews() guard let svc = splitViewController else { return } if svc.isCollapsed { navigationItem.leftBarButtonItem = backButtonItem } else { navigationItem.leftBarButtonItem = svc.displayModeButtonItem } } func dismissAnimated() { _ = navigationController?.navigationController?.popViewController(animated: true) } 

I put this in willLayoutSubviews() instead of viewDidLoad() so that the button is updated adaptively, for example, to change the orientation on the iPhone 7 Plus and change the class size, for example, in view mode with layout on the iPad.

0
source

Here is what I did in the end to open DetailVC if we are minimized (iPhone excludes + sizes) and show / hide MasterVC if we are not minimized (iPad).

 @IBAction func backTouchUp(_ sender: UIButton) { if let splitViewController = splitViewController, !splitViewController.isCollapsed { UIApplication.shared.sendAction(splitViewController.displayModeButtonItem.action!, to: splitViewController.displayModeButtonItem.target, from: nil, for: nil) } else { navigationController?.popViewController(animated: true) } } 
0
source

Source: https://habr.com/ru/post/986810/


All Articles