UISplitViewController: How force to show master po

2019-03-17 07:04发布

In an iPad App i'm using the UISplitViewController. I need to force to show the master popover when the app launch in portrait mode.

Now I'm using this code and it works well on iOS 5.0.

if (UIInterfaceOrientationIsPortrait(self.interfaceOrientation)) {
   if ([[[AppDelegate sharedAppDelegate] splitViewController] respondsToSelector:[[[AppDelegate sharedAppDelegate] btnMenu] action]]) {
      [[[AppDelegate sharedAppDelegate] splitViewController] performSelector:[[[AppDelegate sharedAppDelegate] btnMenu] action]];
   }            
}

But in iOS 5.1 (with the new type of master popover) the behaviour seems to be random. Sometimes the popover shows in fullscreen and sometimes works well.

Some suggestion for 5.1?

8条回答
爷、活的狠高调
2楼-- · 2019-03-17 07:43

Extending on Rob's answer, this works well for me (in viewDidLoad of detail screen):

//If in portrait mode, display the master view
if (UIInterfaceOrientationIsPortrait(self.interfaceOrientation)) {
    [self.navigationItem.leftBarButtonItem.target performSelector:self.navigationItem.leftBarButtonItem.action withObject:self.navigationItem];
}

No need to fetch a separate reference, using self.navigationItem.leftBarButtonItem instead

查看更多
该账号已被封号
3楼-- · 2019-03-17 07:46

No need to keep silly references around to the barButtonItem. Simply call the same target/action. See my answer https://stackoverflow.com/a/25695923/1021430

The target is the split view controller, and the action is toggleMasterVisible:

查看更多
冷血范
4楼-- · 2019-03-17 07:47

For iOS8 the easiest way is with the following:

 self.splitViewController.preferredDisplayMode = UISplitViewControllerDisplayModePrimaryOverlay;

I use this when the app is launched the first time for showing log-in in masterViewController. In all other cases I use

self.splitViewController.preferredDisplayMode = UISplitViewControllerDisplayModeAutomatic
查看更多
来,给爷笑一个
5楼-- · 2019-03-17 07:47

A slightly less hacky variation (swift):

let btn = self.splitViewController!.displayModeButtonItem()
btn.target?.performSelector(btn.action, withObject: btn)
查看更多
Summer. ? 凉城
6楼-- · 2019-03-17 07:49

No suggestion here for 5.1, but one for 8.0:

Now with iOS8, there are a bunch of new methods for UISplitViewController configuration.

In your case, juste set the right value in preferredDisplayMode, for example in the masterViewController viewDidLoad.

Objective-C:

- (void)viewDidLoad {
    // configuring splitviewcontroller
    self.splitViewController.preferredDisplayMode = UISplitViewControllerDisplayModeAllVisible;

    //....
}

Swift:

    override func viewDidLoad() {
        self.splitViewController?.preferredDisplayMode = UISplitViewControllerDisplayMode.AllVisible
    }

But it's of course iOS8 only.

查看更多
我只想做你的唯一
7楼-- · 2019-03-17 08:00

If you need it at app launch, override this method in your detail view controller:

-(BOOL)splitViewController:(UISplitViewController *)svc shouldHideViewController:(UIViewController *)vc inOrientation:(UIInterfaceOrientation)orientation
{
    return NO;
}

However if you then need it to subsequently hide it looks as though the method isn't called, so you'll have to manually hide it.

查看更多
登录 后发表回答