iOS 8 presentationController determine if really is popover

后端 未结 8 942
暖寄归人
暖寄归人 2020-12-15 20:33

I\'m using the new adaptive \"Present As Popover\" capability of iOS 8. I wired up a simple segue in the StoryBoard to do the presentation. It works great on an iPhone 6 P

8条回答
  •  南方客
    南方客 (楼主)
    2020-12-15 21:19

    I tested all solutions presented in this post. Sorry, none works correctly in all cases. For example in iPad split view presentation style can change while dragging split view line, so we need specific notification for that. After few hours of researches i found solution in apple sample (swift): https://developer.apple.com/library/ios/samplecode/AdaptivePhotos/Introduction/Intro.html#//apple_ref/doc/uid/TP40014636

    Here is the same solution in obj-c.

    First in prepareForSegue function set the popoverPresentationController delegate. It can be also set in MyViewController "init", but not in "viewDidLoad" (because first willPresentWithAdaptiveStyle is called before viewDidLoad).

    MyViewController *controller = [segue destinationViewController];
            controller.popoverPresentationController.delegate = (MyViewController *)controller;
    

    Now MyViewController object will receive this notification every time iOS changes presentation style, including first presenting. Here is example implementation which shows/hides "Close" button in navigationController:

    - (void)presentationController:(UIPresentationController *)presentationController
      willPresentWithAdaptiveStyle:(UIModalPresentationStyle)style
             transitionCoordinator:(nullable id)transitionCoordinator {
        if (style == UIModalPresentationNone) {
            // style set in storyboard not changed (popover), hide close button
            self.topViewController.navigationItem.leftBarButtonItem = nil;
        } else {
            // style changed by iOS (to fullscreen or page sheet), show close button
            UIBarButtonItem *closeButton =
                [[UIBarButtonItem alloc] initWithTitle:@"Close" style:UIBarButtonItemStylePlain target:self action:@selector(closeAction)];
            self.topViewController.navigationItem.leftBarButtonItem = closeButton;
        }
    }
    
    - (void)closeAction {
        [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
    }
    

提交回复
热议问题