Uisplitviewcontroller in Portrait on Iphone Shows Detail Vc Instead of Master

UISplitViewController in portrait on iPhone shows detail VC instead of master

Oh man, this was causing me a headache for a few days and could not figure out how to do this. The worst part was that creating a new Xcode iOS project with the master-detail template worked just fine. Fortunately, in the end, that little fact was how I found the solution.

There are some posts I've found that suggest that the solution is to implement the new primaryViewControllerForCollapsingSplitViewController: method on UISplitViewControllerDelegate. I tried that to no avail. What Apple does in the master-detail template that seems to work is implement the new (take a deep breath to say all of this one) splitViewController:collapseSecondaryViewController:ontoPrimaryViewController: delegate method (again on UISplitViewControllerDelegate). According to the docs, this method:

Asks the delegate to adjust the primary view controller and to incorporate the secondary view controller into the collapsed interface.

Make sure to read up on the discussion part of that method for more specific details.

The way that Apple handles this is:

- (BOOL)splitViewController:(UISplitViewController *)splitViewController
collapseSecondaryViewController:(UIViewController *)secondaryViewController
ontoPrimaryViewController:(UIViewController *)primaryViewController {

if ([secondaryViewController isKindOfClass:[UINavigationController class]]
&& [[(UINavigationController *)secondaryViewController topViewController] isKindOfClass:[DetailViewController class]]
&& ([(DetailViewController *)[(UINavigationController *)secondaryViewController topViewController] detailItem] == nil)) {

// Return YES to indicate that we have handled the collapse by doing nothing; the secondary controller will be discarded.
return YES;

} else {

return NO;

}
}

This implementation basically does the following:

  1. If secondaryViewController is what we're expecting (a UINavigationController), and it's showing what we're expecting (a DetailViewController -- your view controller), but has no model (detailItem), then "Return YES to indicate that we have handled the collapse by doing nothing; the secondary controller will be discarded."
  2. Otherwise, return "NO to let the split view controller try and incorporate the secondary view controller’s content into the collapsed interface"

The results are the following for the iPhone in portrait (either starting in portrait or rotating to portrait -- or more accurately compact size class):

  1. If your view is correct
    • and has a model, show the detail view controller
    • but has no model, show the master view controller
  2. If your view is not correct
    • show the master view controller

Clear as mud.

Open UISplitViewController to Master View rather than Detail

Swift

UISplitViewController display master view above detail in portrait orientation is not about showing the Master view, it is about presenting the Detail view in full width, underneath the Master view.

UISplitViewController in portrait on iPhone shows detail VC instead of master is about the principle of the collapse mechanism.

This present answer addresses:

  • Master → Detail (Compact width)

    • iPhone 4s, 5, 5s, SE, 6, 6s, 7 (any orientation)
    • iPod Touch
    • any iPhone Plus (portrait)
  • side-by-side (all other sizes)

    • iPad
    • any iPhone Plus (landscape)

You must set preferredDisplayMode. You would want is .primaryVisible if it existed! Using .allVisible, iOS picks Detail if only 1 view fits (Compact width); in that size, the code below will pick Master.

The trick is to change both the preferredDisplayMode to .allVisible and to return true in collapseSecondary:onto.

class PrimarySplitViewController: UISplitViewController,
UISplitViewControllerDelegate {

override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
self.preferredDisplayMode = .allVisible
}

func splitViewController(
_ splitViewController: UISplitViewController,
collapseSecondary secondaryViewController: UIViewController,
onto primaryViewController: UIViewController) -> Bool {
// Return true to prevent UIKit from applying its default behavior
return true
}
}

UISplitViewController display master view above detail in portrait orientation

Edit: It's not a duplicate. Answer discovered in the comments. The solution is to use preferredDisplayMode on UISplitViewController and setting it to UISplitViewControllerDisplayModePrimaryOverlay

Left the original answer for context to the comments and posterity.


Original Answer

This is a duplicate of this: UISplitViewController in portrait on iPhone shows detail VC instead of master

For reference, the solution in that case was to have the view controller that implements UISplitViewControllerDelegate use the following code:

- (BOOL)splitViewController:(UISplitViewController *)splitViewController
collapseSecondaryViewController:(UIViewController *)secondaryViewController
ontoPrimaryViewController:(UIViewController *)primaryViewController {

if ([secondaryViewController isKindOfClass:[UINavigationController class]]
&& [[(UINavigationController *)secondaryViewController topViewController] isKindOfClass:[DetailViewController class]]
&& ([(DetailViewController *)[(UINavigationController *)secondaryViewController topViewController] detailItem] == nil)) {

// Return YES to indicate that we have handled the collapse by doing nothing; the secondary controller will be discarded.
return YES;

} else {

return NO;

}
}

Show Master on UISplitViewController by Default

After some experimentation, it seems that overriding CollapseSecondViewController on the delegate will work though I'm not yet convinced it's the proper solution.

using Foundation;
using System;
using UIKit;

namespace MasterDetailTest
{
public class SplitViewControllerDelegate : UISplitViewControllerDelegate
{
public override bool CollapseSecondViewController(UISplitViewController splitViewController, UIViewController secondaryViewController, UIViewController primaryViewController)
{
return true;
}
}

public partial class MainPageSplitViewController : UISplitViewController
{
public MainPageSplitViewController (IntPtr handle) : base (handle)
{
this.Delegate = new SplitViewControllerDelegate();
}

public override void ViewDidLoad()
{
base.ViewDidLoad();

// When implemented in my project, I found I needed to set this
// or the delegate would not be called.
this.SetNeedsFocusUpdate();
}

}
}

UISplitViewController always show master view in iPad portrait mode iOS 9

Subclass UISplitViewController

There is no need to specifically track orientation changes: Master and Detail will still be displayed in sequence on iPhone in portrait mode, and most iPhone in landscape mode.

preferredDisplayMode:.allVisible only affects the modes where both views can be simultaneously visible.

Swift

class SplitViewController: UISplitViewController {

override func viewDidLoad() {
super.viewDidLoad()

preferredDisplayMode = .allVisible
}
}

Obj-C

- (void)viewDidLoad {
[super viewDidLoad];
self.preferredDisplayMode = UISplitViewControllerDisplayModeAllVisible;

}

iPad Portrait & iPhone 8 Plus Landscape

Sample Image

Swift UISplitViewController - unable to present master view before detail view (iOS 14, Xcode 12.4)

Returning .primary should solve your issue.

@available(iOS 14.0, *)
public func splitViewController(_ svc: UISplitViewController, topColumnForCollapsingToProposedTopColumn proposedTopColumn: UISplitViewController.Column) -> UISplitViewController.Column {
return .primary
}

[From Documentation]
Asks the delegate to provide the column to display after the split view interface collapses.

When the split view controller transitions from a horizontally regular to a horizontally compact size class, it calls this method and asks you for the column to display when that transition is complete. Use this method to customize the view controller you’re collapsing to.

How to let UISplitViewController on Storyboard show master view first?

In order to enable the master view to collapse on iPhone by default, override some delegate methods:

class SplitViewController : UISplitViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
}
}

extension SplitViewController : UISplitViewControllerDelegate {

// The default for this is .secondary!!
@available(iOS 14.0, *)
public func splitViewController(_ svc: UISplitViewController,
topColumnForCollapsingToProposedTopColumn
proposedTopColumn: UISplitViewController.Column) -> UISplitViewController.Column {
return .primary
}

// default is false!
public func splitViewController(_ splitViewController: UISplitViewController,
collapseSecondary secondaryViewController:UIViewController,
onto primaryViewController:UIViewController) -> Bool {
return true
}
}

Previously you'd use preferredDisplayMode = .primaryOverlay, but this is now deprecated. The code works for iOS 14 and is backwards compatible for earlier iOS versions also.

UISplitViewController in portrait on iPhone always show master and detail in iOS 8

UISplitViewController use show side-by-side only in horizontally regular environment (new TraitCollection size)

The split view controller determines the arrangement of its child view controllers based on the available space. In a horizontally regular environment, the split view controller presents its view controllers side-by-side whenever possible. In a horizontally compact environment, the split view controller acts more like a navigation controller, displaying the primary view controller initially and pushing or popping the secondary view controller as needed. You can also ask the split view controller to prefer a specific arrangement by assigning a value to the preferredDisplayMode property.

Solution.
You have to change TraitCollection of SplitViewController. How do this:

  • Create a ViewController and add your SplitViewController as a child.
  • Override traitCollection with size .Regular for your child (UISplitViewController)

ViewController Wrapper

class TraitOverrideViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
configureSplitVC()
}


private func configureSplitVC() {

let splitVC = self.childViewControllers[0] as UISplitViewController
setOverrideTraitCollection(UITraitCollection(horizontalSizeClass: .Regular), forChildViewController: splitVC)
}
}

In iOS 8 UISplitViewController uses Adaptive User Interfaces and TraitCollections to show its content.

It shows different style depending on the size of and type of the view. You can change by technic I explain above.

You can get more info in WWDC video about Building Adaptive Apps with UIKit



Related Topics



Leave a reply



Submit