UIPopoverPresentationController on iPhone with iOS 10

前端 未结 4 625
刺人心
刺人心 2021-01-14 09:05

I\'m trying to display a ViewController as a popover on an iPhone. I have already been through several answers on SO and the rest of the web but none have worked so far. I w

4条回答
  •  感情败类
    2021-01-14 09:36

    The solutions above no longer work in recent iOS versions 12 and above. To get it working again, override -modalPresentationStyle within the viewController to be presented as a popover and return UIModalPresentationPopover. Additionally provide a popoverPresentationController.delegate, implement adaptivePresentationStyleForPresentationController:traitCollection: and return UIModalPresentationNone.

    @interface PopoverViewController : UIViewController
    @end
    
    @implementation PopoverViewController
    
    - (UIModalPresentationStyle)modalPresentationStyle
    {
        return UIModalPresentationPopover;
    }
    
    @end
    
    @interface ViewController ()
    @end
    
    @implementation ViewController
    
    - (IBAction)openPopover:(id)sender
    {
        UIViewController* testVC = [[PopoverViewController alloc] init];
        testVC.view.backgroundColor = UIColor.yellowColor;
        UIPopoverPresentationController* popPresenter = [testVC popoverPresentationController];
        popPresenter.permittedArrowDirections = UIPopoverArrowDirectionUp;
        popPresenter.delegate = self;
        popPresenter.sourceView = sender;
        popPresenter.sourceRect = [sender bounds];
        [self presentViewController:testVC animated:YES completion:^{}];
    }
    
    #pragma mark protocol (UIPopoverPresentationControllerDelegate)
    
    - (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller traitCollection:(UITraitCollection *)traitCollection
    {
        return UIModalPresentationNone;
    }
    
    @end
    

提交回复
热议问题