Open UISplitViewController to Master View rather than Detail

前端 未结 7 1183
北海茫月
北海茫月 2020-12-04 13:14

I have a split-view interface with a target iPhone 6 application. On the first launch of the application, it opens to the Detail View; I would like it to open to the Master

7条回答
  •  臣服心动
    2020-12-04 13:55

    This is an oldish question and none of the answers were for Objective C, and even when I ported the Swift answers, none worked for me. One was close, by @SwiftArchitect.

    But he recommended setting the content mode to .allVisible (UISplitViewControllerDisplayModeAllVisible in Objective C) - this makes the master view display all the time, splitting the view into master on one side, detail on the other. Which is kinda cool, but the OP asked specifically to display the master view on initial launch, which is what I needed to do.

    The change was to use UISplitViewControllerDisplayModePrimaryOverlay for the display mode.

    This answer is for Xcode 9.4.1, deployment target 11.4.

    Here is MasterViewController.h - you need to add UISplitViewControllerDelegate in the protocols declaration:

    #import 
    #import 
    #import "MasterDetailDemo+CoreDataModel.h"
    
    @class DetailViewController;
    
    @interface MasterViewController : UITableViewController
    
    
    @property (strong, nonatomic) DetailViewController *detailViewController;
    
    @property (strong, nonatomic) NSFetchedResultsController *fetchedResultsController;
    @property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;
    
    @end
    

    And then in your MasterViewController.m, you need to set the split view controller delegate and the content mode in ViewDidLoad, and following along with @SwiftArchitect's answer, to also add the split view controller delegate method:

    - (void)viewDidLoad {
    
        [super viewDidLoad];
    
        // needed to "slide out" MasterView on startup on iPad
        self.splitViewController.delegate = self;
        self.splitViewController.preferredDisplayMode = UISplitViewControllerDisplayModePrimaryOverlay;
    
        self.navigationItem.leftBarButtonItem = self.editButtonItem;
    
        UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)];
    
        self.navigationItem.rightBarButtonItem = addButton;
    
        self.detailViewController = (DetailViewController *)[[self.splitViewController.viewControllers lastObject] topViewController];
    }
    
    // split view delegate method
    - (BOOL)splitViewController:(UISplitViewController *)splitViewController collapseSecondaryViewController:(UIViewController *)secondaryViewController ontoPrimaryViewController:(UIViewController *)primaryViewController {
        return true;
    }
    

    NOTE: After some testing, I found that the split view delegate method and the split view protocol was not necessary. Without it, it appears to work exactly the same. Perhaps this is a result of changes in iOS since the question was originally asked and answered.

    I got it working fine just by putting this line in my ViewDidLoad method:

    self.splitViewController.preferredDisplayMode = UISplitViewControllerDisplayModePrimaryOverlay;
    

提交回复
热议问题